从php执行Powershell脚本

时间:2011-03-15 20:01:17

标签: 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>";

输出:

$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,但它仍然是一样的。

4 个答案:

答案 0 :(得分:8)

看起来您的命令被单引号包围。我想如果你删除它们,你的命令应该运行。

shell_exec返回您运行的命令的输出。要进一步诊断,请将输出存储在变量中,然后将其打印出来:

$output= shell_exec($runCMD);
echo( '<pre>' );
echo( $output );
echo( '</pre>' );

确保启用正在运行的脚本。默认情况下,该功能处于关闭状态。您必须在要运行PowerShell脚本的每台计算机上启用脚本的执行。运行about help_signing以获取更多信息。

Microsoft建议运行Set-ExecutionPolicy RemoteSigned -Scope LocalMachine。这允许计算机上的所有用户帐户无问题地运行本地脚本,但需要确认才能运行从Internet下载的脚本。这需要在管理提示符中运行。如果您运行的是64位操作系统,则需要从64位和32位shell执行此操作。

答案 1 :(得分:2)

使用“-executionPolicy Unrestricted”以及“powershell.exe”命令。因此命令将是:

powershell.exe -executionPolicy Unrestricted

然后肯定会有效。

答案 2 :(得分:1)

在另一个网站上发现这个并认为我会传递它:

  

我正在调试一个使用Windows API(创建一个孩子)的程序   使用重定向输入和输出进行处理以捕获标准输出   微软的Windows PowerShell。

     

传递给PowerShell的脚本(-File开关)没有执行和   PowerShell刚被绞死,直到被任务管理器杀死。

     

事实证明,您需要使用未记录的参数“-InputFormat   无”:

     

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -InputFormat none -File file.ps1

这对我有用。

答案 3 :(得分:0)

要从PHP执行脚本文件,您应遵循以下示例:

您应该以简单的PowerShell脚本开始。创建一个名称为“ test.ps1 ”的文本文件。

现在在此文件中键入以下脚本:

Get-Process

将下面的代码放在一个名为“ test.php ”的PHP文件中。请记住,要在以下示例“ C:/PATH/TO/test.ps1” 中使用您自己的脚本文件的绝对路径来更新文件路径。

echo "<pre>";
echo Shell_Exec('powershell -InputFormat none -ExecutionPolicy ByPass -NoProfile -Command "& { . \"C:/PATH/TO/test.ps1\"; }"');
echo "</pre>";

上面的代码将输出所有正在运行的进程的列表。

请注意,我正在Windows PC上运行脚本,并且由于文件路径而收到错误消息。因此,我用正斜杠替换了文件路径中的所有反斜杠。

以下参数值得注意:

  1. -InputFormat无-由于PowerShell中的错误,脚本将永远无法完成运行或退出。此参数提供解决此错误的方法。
  2. -ExecutionPolicy ByPass -如果没有此参数,则代码将抛出低权限错误消息。
  3. -NoProfile -使用此参数,您的脚本将运行得更快,更可预测。
  4. -Command -通过“ -Command”参数而不是“ -File”参数加载脚本文件,您将具有更大的灵活性。 例如,如果您需要在脚本中调用一个函数并将参数发送给该函数,则需要使用“ -Command”参数。

这里是如何在PowerShell脚本文件中调用函数“ my-function” 并传递参数“-myParameter” “的示例-myOtherParameter” 分别具有值“ 10” “ 15”

echo "<pre>";
echo Shell_Exec('powershell -InputFormat none -ExecutionPolicy ByPass -NoProfile -Command "& { . \"C:/PATH/TO/test.ps1\"; my-function -myParameter 10 -myOtherParameter 15 }"');
echo "</pre>";