raymak Posted September 7, 2014 Share Posted September 7, 2014 <?php error_reporting(E_ALL); $psPath = "powershell.exe"; $psDir = "C:\\wamp\\www\\ps\\"; $psScript = "SampleHTML.ps1"; $runScript = $psDir. $psScript; $prem = "-Action enable"; $runCMD = $psPath. " " .$runScript. " " .$prem; //var_dump($runCMD); $output = exec($runCMD); echo $output; ?> Hello, I am working on a small project to get results from powershell script by using PHP. For some reason in PHP logs I get Exec unable to fork. Above is the script I wrote to execute powershell script within php. My webserver is IIS 7, and app pool is using a domain user that has full rights for Powershell to execute and get remote server results. Quote Link to comment https://forums.phpfreaks.com/topic/290906-execute-powershell-script-within-php-in-iis/ Share on other sites More sharing options...
requinix Posted September 7, 2014 Share Posted September 7, 2014 exec() uses a shell so you also need rights on cmd.exe. Alternatively you can use the proc_* functions; they're harder to use but are much more powerful, and can* execute programs directly without using a shell. * with $other_options's bypass_shell=true Quote Link to comment https://forums.phpfreaks.com/topic/290906-execute-powershell-script-within-php-in-iis/#findComment-1490273 Share on other sites More sharing options...
raymak Posted September 8, 2014 Author Share Posted September 8, 2014 Thanks Requinix. I will try out proc_open function and see how it goes. Quote Link to comment https://forums.phpfreaks.com/topic/290906-execute-powershell-script-within-php-in-iis/#findComment-1490321 Share on other sites More sharing options...
raymak Posted September 8, 2014 Author Share Posted September 8, 2014 <?php //error_reporting(E_ALL); $psPath = "powershell.exe"; $psDir = "C:\\wamp\\www\\ps\\"; $psScript = "SampleHTML.ps1"; $runScript = $psDir. $psScript; $prem = "-Action enable"; $runCMD = $psPath. " " .$runScript. " " .$prem; var_dump($runCMD); $output = proc_open($runCMD); $exitcode = proc_close($output); echo $exitcode; ?> string(59) "powershell.exe C:\wamp\www\ps\SampleHTML.ps1 -Action enable" Warning: proc_open() expects at least 3 parameters, 1 given in C:\wamp\www\powershell.php on line 13 Warning: proc_close() expects parameter 1 to be resource, boolean given in C:\wamp\www\powershell.php on line 15 Hi, I tried the proc_open function. I think I am missing some parameters to make it functional. Here is the error I am receiving: Here is the modified code: Quote Link to comment https://forums.phpfreaks.com/topic/290906-execute-powershell-script-within-php-in-iis/#findComment-1490382 Share on other sites More sharing options...
requinix Posted September 8, 2014 Share Posted September 8, 2014 (edited) proc_open proc_close You can't just swap the function names around. Read the documentation on how to use them. [edit] Additionally, IIRC, you have to do a little more with PowerShell for everything to run smoothly. Such as send an "exit" command to get the process to quit by itself. Edited September 8, 2014 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/290906-execute-powershell-script-within-php-in-iis/#findComment-1490388 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.