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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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