简体   繁体   English

从PHP运行Powershell命令

[英]Running Powershell commands from PHP

I am trying to run a PowerShell command from inside a php file on my webserver. 我正在尝试从Web服务器上的php文件内部运行PowerShell命令。

Here is my php code: 这是我的PHP代码:

<?php
$query = shell_exec('C:\\Windows\\syswow64\\WindowsPowerShell\\v1.0\\powershell.exe -command Get-Host');
echo $query;
?> 

The result I get is HTTP Error 500 - C:\\PHP\\php-cgi.exe - The FastCGI process exceeded configured activity timeout - Error Code 0x80070102 我得到的结果是HTTP错误500-C:\\ PHP \\ php-cgi.exe-FastCGI进程超出了配置的活动超时-错误代码0x80070102

To perform a test to verify shell_exec and php were functioning (that I hadn't screwed something up when setting up the environment). 要执行测试以验证shell_exec和php是否正常运行(设置环境时我没有搞错什么)。 I also tried this php: 我也试过这个php:

<?php
$query = shell_exec('dir c:\\');
echo $query;
?>

This worked, the page source looked exactly like you'd expect if you had run the dir c:\\ command from a command prompt in windows. 这样可以正常工作,如果您在Windows的命令提示符下运行dir c:\\命令,则页面源代码看起来与您期望的完全一样。

I've looked at several other articles here on stackoverflow, as well as on technet, and can't seem to identify what is causing this to time out. 我在这里也查看了关于stackoverflow和Technet的其他几篇文章,似乎无法确定是什么原因导致超时。 When I run the same command from the command prompt the result is very quick. 当我从命令提示符处运行同一命令时,结果很快。

Here are the results from executing the same command manually at the command prompt (again what you'd expect): 这是在命令提示符处再次手动执行同一命令的结果(与您期望的一样):

C:\>C:\Windows\syswow64\WindowsPowerShell\v1.0\powershell.exe -command Get-Host


Name             : ConsoleHost
Version          : 2.0
InstanceId       : 9a24a372-f0a3-4a79-b9fd-1e180e9a8069
UI               : System.Management.Automation.Internal.Host.InternalHostUserI
               nterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

A little about my environment: 关于我的环境的一些信息:

I'm running IIS 7.5 on a Windows 2008 R2 SP1 Standard Server. 我在Windows 2008 R2 SP1标准服务器上运行IIS 7.5。

The server is standalone (not part of a domain). 该服务器是独立的(不是域的一部分)。

I have PHP 5.5.9 installed, and working. 我已经安装了PHP 5.5.9,并且可以正常工作。 phpinfo() returns the expected page without any noticeable errors. phpinfo()返回预期的页面,没有任何明显的错误。

Any help in getting me on track with executing powershell commands from php is much appreciated! 非常感谢您帮助我从php执行powershell命令。

尝试这个 :

$query = shell_exec('C:\\Windows\\syswow64\\WindowsPowerShell\\v1.0\\powershell.exe -command Get-Host < NUL');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM