How to execute a Powershell script in PHP and get the result?

Learning how Php interacts with Powershell Try the simplest script:

<?php
$query = shell_exec("powershell.exe -File E:\test.ps1");
echo $query; 
?>

In script test.ps1 - for example, "Test Connection Server"

The need for an answer in Powershell returned to the Php page, but in response to the white paper ... Please tell me some solution to this problem. Does not have shell_exec. Could there be other options?

+4
source share
1 answer

You can save the output of powershell script in a variable and then repeat it. Just change $psDIRto your PowerShell path (e.g. %SystemRoot%\system32\WindowsPowerShell\v2.0\)

<?php
$psPath = "powershell.exe";
$psDIR = "PathToPowrshell";
$psScript = "E:\test.ps1";
$runScript = $psDIR. $psScript;
$runCMD = $psPath." ".$runScript;
$output= shell_exec($runCMD);
echo( '<pre>' );
echo( $output );
echo( '</pre>' );
?>
+2
source

Source: https://habr.com/ru/post/1684217/


All Articles