technor
technor

Reputation: 71

How execute Powershell script in PHP and output result?

Studying the interaction of Php with Powershell Try the simplest script:

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

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

Need to the answer in Powershell returned to the page Php, but in response to a white paper... Please tell me some solution for this problem.. do Not have shell_exec. There may be other options?

Upvotes: 4

Views: 3645

Answers (1)

AutoTester213
AutoTester213

Reputation: 2862

You can store the output of powershell script in a variable and then echo it. Just change $psDIR to 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>' );
?>

Upvotes: 2

Related Questions