4

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?

1 Answer 1

2

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>' );
?>
Sign up to request clarification or add additional context in comments.

5 Comments

$runCMD isn't defined and $psPath is never used?
Good spot forgot to definy it
Ahh, now it makes sense :-D
<?php $psPath = "powershell.exe"; $psDIR = "C:\Windows\SysWOW64\WindowsPowerShell\v1.0\"; $psScript = "E:\test2.ps1"; $runScript = $psDIR. $psScript; $runCMD = $psPath." ".$runScript; $output= shell_exec($runCMD); echo( '<pre>' ); echo( $output ); echo( '</pre>' ); ?>
Do you get any errors? also make sure all your paths are correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.