从 php 执行 Powershell 脚本 [英] executing a Powershell script from php

查看:45
本文介绍了从 php 执行 Powershell 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 PHP 执行 powershell 脚本,但它似乎不起作用.

脚本newEvent.ps1"在 Exchange 服务器上创建一个事件.

$psPath = "powershell.exe";$psDIR = "C:\wamp\www\ant\assets\ps\";$psScript = "newEvent.ps1";$runScript = $psDIR.$ps脚本;$runCMD = $psPath." ".$runScript." 2>&1";echo "$psPath $psPath 
";echo "$psDIR $psDIR
";echo "$psScript $psScript
";echo "$runScript $runScript
";echo "$runCMD $runCMD
";exec($runCMD,$out,$ret);echo "

";打印_r($out);打印_r($ret);echo "</pre>";

它输出:

$psPath powershell.exe$psDIR C:wampwwwantassetsps$psScript newEvent.ps1$runScript C:wampwwwantassetsps
ewEvent.ps1$runCMD powershell.exe C:wampwwwantassetsps
ewEvent.ps1 2>&1大批([0] =>文件 C:wampwwwantassetsps
ewEvent.ps1 无法加载,因为执行[1] =>在此系统上禁用脚本离子.请参阅get-help about_signing"[2] =>更多细节.[3] =>在行:1 字符:39[4] =>+ C:wampwwwantassetsps
ewEvent.ps1 <<<<[5] =>+ CategoryInfo : NotSpecified: (:) [], PSSecurityException[6] =>+ FullQualifiedErrorId:运行时异常[7] =>)

如果我在命令行上运行 powershell.exe C:wampwwwantassetsps ewEvent.ps1,它工作正常.

这是我第一次尝试这样的事情.我运行了 Set-ExecutionPolicy RemoteSigned -Scope LocalMachine 但它仍然给我同样的错误.事实上,我运行了Set-ExecutionPolicy unristricted,但还是一样.

解决方案

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

shell_exec 返回您运行的命令的输出.为了进一步诊断,将输出存储在一个变量中,然后打印出来:

$output = shell_exec($runCMD);echo '

'.$输出.'</pre>';

确保您启用正在运行的脚本.该功能默认关闭.您必须在要运行 PowerShell 脚本的每台机器上启用脚本的执行.运行 about help_signing 以获取更多信息.

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

I'm trying to execute a powershell script from PHP, but it does not seem to work.

The script 'newEvent.ps1' creates an event on the Exchange server.

$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>";

It outputs:

$psPath powershell.exe
$psDIR C:wampwwwantassetsps
$psScript newEvent.ps1
$runScript C:wampwwwantassetsps
ewEvent.ps1
$runCMD powershell.exe C:wampwwwantassetsps
ewEvent.ps1 2>&1

Array
(
    [0] => File C:wampwwwantassetsps
ewEvent.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:wampwwwantassetsps
ewEvent.ps1 <<<<
    [5] =>     + CategoryInfo          : NotSpecified: (:) [], PSSecurityException
    [6] =>     + FullyQualifiedErrorId : RuntimeException
    [7] => 
)

If I run powershell.exe C:wampwwwantassetsps ewEvent.ps1 on the command-line, it works fine.

This is the first time im attempting something like this. I ran Set-ExecutionPolicy RemoteSigned -Scope LocalMachine but it still gives me the same error. In fact I ran Set-ExecutionPolicy unristricted, but it's still the same.

解决方案

It looks like your command is surrounded by single-quotes. I think if you remove them, your command should run.

shell_exec returns the output from the command you run. To further diagnose, store the output in a variable, then print it out:

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

Make sure you enable running scripts. That capability is turned off by default. You have to enable the execution of scripts on each machine you want to run PowerShell scripts. Run about help_signing for more information.

Microsoft recommends running Set-ExecutionPolicy RemoteSigned -Scope LocalMachine. This allows all user accounts on a machine to run local scripts without issue, but requires confirmation to run scripts downloaded from the internet. This needs to be run in an administrative prompt. If you are running a 64-bit operating system, you'll need to do this from both a 64-bit and 32-bit shell.

这篇关于从 php 执行 Powershell 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆