简体   繁体   中英

Running Powershell commands from PHP

I am trying to run a PowerShell command from inside a php file on my webserver.

Here is my php code:

<?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

To perform a test to verify shell_exec and php were functioning (that I hadn't screwed something up when setting up the environment). I also tried this 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.

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. 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.

The server is standalone (not part of a domain).

I have PHP 5.5.9 installed, and working. phpinfo() returns the expected page without any noticeable errors.

Any help in getting me on track with executing powershell commands from php is much appreciated!

尝试这个 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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