Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browser Hangs Executing PHP that runs a Powershell

I am trying to run a simple PHP that runs a powershell script, if I use this code I get the results in a command window, but I get an empty array in the browser:

<?php 
exec("powershell C:\\Inetpub\\wwwroot\\my_shell.ps1 < NUL", $output);

echo "<pre>";
print_r($output);
echo "</pre>";
?>

I believe the NUL will discard the output, however it works in the browser found it on [this Fourm][1]

If I use this code, without the NUL I will get the results on a command window but if I run the script in the browser will keep loading forever and it will never give me any results:

exec("powershell C:\\Inetpub\\wwwroot\\emsrDev\\manual_shell.ps1", $output);

Same results if I do it this way:

$output = shell_exec("powershell C:\\Inetpub\\wwwroot\\emsrDev\\manual_shell.ps1");

The powershell Script runs fine if I ran it independently:

$cmd = "cmd.exe";
&$cmd "/C echo update tasks set last='manual' where id='8'; | sqlplus vvv/www@xxx";

So I need to execute this in a browser and get the output.

like image 205
OmarL Avatar asked Nov 13 '22 05:11

OmarL


1 Answers

The powershell process isn't terminating, which causes the browser to hang. Add the following to the end of your script

Stop-Process -processname powershell*
like image 167
mitchimus Avatar answered Dec 06 '22 10:12

mitchimus