Jump to content

Execute Powershell script within PHP in IIS


raymak

Recommended Posts

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

 

 

 

 

 

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

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

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.

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.