通过php执行powershell命令


Execute powershell command via php

我有以下问题:

我有一个examplep服务器正在运行,我希望它执行一个powershell。php触发一个.bat文件,该文件包含以下代码:

@echo
cd C:'OpenBR'bin
start /WAIT br -algorithm FaceRecognition -compare C:'xampp'htdocs'upload C:'xampp'htdocs'DP C:'xampp'htdocs'results'result.csv

start  /WAIT C:'xampp'htdocs'CSVconvert'sortieren.ps1 
start  /WAIT C:'xampp'htdocs'CSVconvert'Removedouble.ps1
start  /WAIT C:'xampp'htdocs'CSVconvert'remove_path.ps1
start  /WAIT C:'xampp'htdocs'CSVconvert'remove_foo.ps1
start  C:'xampp'htdocs'CSVconvert'remove_quoatation.ps1

第一部分工作得很好,直到我想执行powershell"atteren.ps1"。当我手动运行批处理时,它会执行并完成任务,当通过php触发时,它不会。我在x86和x64外壳中都设置了"set ExecutionPolicy Unrestricted"。我只是感到困惑,因为正常的命令行可以工作,而powershell不可以,即使在不受限制地设置它之后也是如此。

我查看了从php执行Powershell脚本和Windows 7上的PowerShell:为常规用户设置ExecutionPolicy但无法解决问题。我错过了什么?

在其中运行这些命令的会话没有与使用PowerShell手动运行这些命令时相同的环境变量。您必须指定powershell可执行程序的绝对路径以及要运行的脚本,以便找到它们。

start /WAIT C:'Windows'System32'WindowsPowerShell'v1.0'powershell.exe C:'xampp'htdocs'CSVconvert'sortieren.ps1

由于问题出在环境上,我认为您可能会从自动处理该方面的包中受益。这里有一个项目,允许PHP获得一个真正的Powershell并与之动态交互。在这里获取:https://github.com/merlinthemagic/MTS

下载后,您只需使用以下代码:

$shellObj    = 'MTS'Factories::getDevices()->getLocalHost()->getShell('powershell');
$strCmd1   = 'first command from first script';
$return1  = $shellObj->exeCmd($strCmd1);
$strCmd2   = 'second command from first script';
$return2  = $shellObj->exeCmd($strCmd2);

您可以单独触发每个命令并处理返回,而不是触发单个脚本。您可以对$shellObj发出任何您喜欢的命令,该环境在PHP脚本的整个生命周期中都得到维护。