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.

 

 

 

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by requinix
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.