Jump to content

PHP and Powershell


slowfib

Recommended Posts

I'm using PHP on a windows box to execute some powershell scripts, my only problem is that the browser hangs on the request.

The browser will continue to hang until I goto Task Manager and kill Powershell.exe, then my results are returned as expected.

 

I've tried to execute an "exit" command after executing my Powershell commands, but that doesn't seem to work.

 

 

Anyone used Powershell before, or have any ideas on how I could handle this otherwise?

 

Here's my code:

$output = array();
exec("powershell get-service", $output);
print_r($output);

 

 

Thank you!

Link to comment
https://forums.phpfreaks.com/topic/211188-php-and-powershell/
Share on other sites

Hmmmm... executing the batch file still hangs the browser and Powershell keeps on running in the background.

But if I execute the batch file inside of windows, Powershell.exe stops running as soon as the batch file is done executing.

 

I'm wondering if I can pipe an exit in Powershell to exit once it's finished or something...

 

 

Here's my code now:

$output = array();
$filename = "C:\\temp\\powershell.bat";
$handle = fopen($filename, "w");
fwrite($handle, "powershell.exe get-service > C:\\Temp\\powershell-output.txt\nexit");
fclose($handle);

exec("C:\\temp\\powershell.bat");
unlink($filename);
$filename = "C:\\Temp\\powershell-output.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
unlink($filename);
print_r($contents);

Link to comment
https://forums.phpfreaks.com/topic/211188-php-and-powershell/#findComment-1101327
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.