hes*_*anh 12 php powershell exchange-server shell-exec
我正在尝试从PHP执行powershell脚本,但它似乎不起作用.
脚本'newEvent.ps1'在Exchange服务器上创建一个事件.
$psPath = "powershell.exe";
$psDIR = "C:\\wamp\\www\\ant\\assets\\ps\\";
$psScript = "newEvent.ps1";
$runScript = $psDIR. $psScript;
$runCMD = $psPath." ".$runScript." 2>&1";
echo "\$psPath $psPath <br>";
echo "\$psDIR $psDIR <br>";
echo "\$psScript $psScript <br>";
echo "\$runScript $runScript <br>";
echo "\$runCMD $runCMD <br>";
exec( $runCMD,$out,$ret);
echo "<pre>";
print_r($out);
print_r($ret);
echo "</pre>";
Run Code Online (Sandbox Code Playgroud)
它输出:
$psPath powershell.exe
$psDIR C:\wamp\www\ant\assets\ps\
$psScript newEvent.ps1
$runScript C:\wamp\www\ant\assets\ps\newEvent.ps1
$runCMD powershell.exe C:\wamp\www\ant\assets\ps\newEvent.ps1 2>&1
Array
(
[0] => File C:\wamp\www\ant\assets\ps\newEvent.ps1 cannot be loaded because the execut
[1] => ion of scripts is disabled on this system. Please see "get-help about_signing"
[2] => for more details.
[3] => At line:1 char:39
[4] => + C:\wamp\www\ant\assets\ps\newEvent.ps1 <<<<
[5] => + CategoryInfo : NotSpecified: (:) [], PSSecurityException
[6] => + FullyQualifiedErrorId : RuntimeException
[7] =>
)
Run Code Online (Sandbox Code Playgroud)
如果我powershell.exe C:\wamp\www\ant\assets\ps\newEvent.ps1在命令行上运行,它运行正常.
这是我第一次尝试这样的事情.我跑了,Set-ExecutionPolicy RemoteSigned -Scope LocalMachine但它仍然给我同样的错误.事实上我跑了Set-ExecutionPolicy unristricted,但它仍然是一样的.
看起来您的命令被单引号包围.我想如果你删除它们,你的命令应该运行.
shell_exec返回您运行的命令的输出.要进一步诊断,请将输出存储在变量中,然后将其打印出来:
$output= shell_exec($runCMD);
echo( '<pre>' );
echo( $output );
echo( '</pre>' );
Run Code Online (Sandbox Code Playgroud)
确保启用运行脚本.默认情况下,该功能处于关闭状态.您必须在要运行PowerShell脚本的每台计算机上启用脚本执行.运行about help_signing以获取更多信息.
Microsoft建议运行Set-ExecutionPolicy RemoteSigned -Scope LocalMachine.这允许计算机上的所有用户帐户无问题地运行本地脚本,但需要确认才能运行从Internet下载的脚本.这需要在管理提示符中运行.如果您运行的是64位操作系统,则需要从64位和32位shell执行此操作.