<!doctype html>

 <html>

   <head>

     <title>
      Execute PowerShell via PHP
     </title>

   </head>

   <body>

     <h1>
      Execute PowerShell via PHP
     </h1>

 <?php

//-Constants------------------------------------------------------------
  const OUTPUT_BUFFER = 2;
  const CrLf = "\r\n";

//-Variables------------------------------------------------------------
  $PSCode = "";
  $Result = "";

//-Sub ExecPS-----------------------------------------------------------
  function ExecPS() {

    global $PSCode;
    global $Result;

    try {
      $PS = new COM("SAPIEN.ActiveXPoSHV3");
    }
    catch (com_exception $exc) {
      $Result = "Can't create PowerShell object";
      return;
    }

    if ($PS->IsPowerShellInstalled() == false) {
      $Result = "PowerShell not installed";
      return;
    }

    if ($PS->Init(false)  <> 0) {
      $Result = "PowerShell init failed";
      return;
    }

    $PS->OutputMode = OUTPUT_BUFFER;
    $PS->OutputWidth = 256;

    $PS->Execute($PSCode);
    $Result = $PS->OutputString();
    $PS->ClearOutput();

    $PS = null;

  }

//-Sub Main-------------------------------------------------------------
  function Main() {

    global $PSCode;

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
      $PSCode = $_POST["pscode"];
      ExecPS();
    }
    else {
      $PSCode = "Get-Process | Format-Table";
    }
  }

//-Main-----------------------------------------------------------------
  Main();

?>

     <table>
       <tr>
         <td>
           <form method="POST">
             <textarea name="pscode" cols="80" rows="25" wrap="off"
              style="overflow:scroll;"> <?php echo $PSCode; ?> </textarea>
             <br />
             <input type="submit" name="execute" value="Execute"
              style="width:585px;">
           </form>
         </td>
         <td valign="top">
           <textarea name="result" cols="80" rows="25" wrap="off"
            style="overflow:scroll;"> <?php echo $Result; ?> </textarea>
         </td>
       </tr>
     </table>

   </body>	

 </html>