我正在尝试从 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>";
它输出:
$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] =>
)
如果我跑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>' . $output . '</pre>';
确保启用运行脚本。该功能默认处于关闭状态。您必须在要运行 PowerShell 脚本的每台计算机上启用脚本执行。跑步about help_signing
了解更多信息。
微软建议运行Set-ExecutionPolicy RemoteSigned -Scope LocalMachine
。这允许计算机上的所有用户帐户毫无问题地运行本地脚本,但需要确认才能运行从互联网下载的脚本。这需要在管理提示符下运行。如果您运行的是 64 位操作系统,则需要从 64 位和 32 位 shell 执行此操作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)