Calling powershell script from php

Hi! I have powershell installed on a Linux. I’m trying to invoke a small powershell script without success from a webpage written in php. The shell_exec can be bash, perl, python. They all work. But powershell, no luck. The script is working fine directly from the command line (run as apache). I’ve tried a few options when calling pwsh. Still no luck.

echo “Start”;
ini_set(‘max_execution_time’, 300);
$output= shell_exec(’/bin/pwsh -nol -c /usr/local/sysadm/root_cmd/drs.ps1 -vc test1 -vm vmtest’);
echo( ‘

’ );
echo( $output );
echo( ‘
’ );
echo “End”;


Have you installed PowerShell on your Linux distro? Providing a link to PowerShell Core with Linux distro, confirm your distro is supported.

[root@slpcfg0269 ~]# pwsh -version
PowerShell 6.1.1

More detail about my issue. I can see the powershell script running for a second in the process list. It runs for about a second, then stops.

It should run for at least 5-7 seconds and there’s no output in my browser. If I replace the powershell script by a simple bash, or python script I get my outout in the browser page.





Take a look at this and let me know if this works. Unfortunately I don’t have a linux machine spun up to test.


< ?php 
echo "Start";
ini_set('max_execution_time', 300);
$output= shell_exec('/bin/pwsh -nol -c {/usr/local/sysadm/root_cmd/drs.ps1 -vc test1 -vm vmtest}');
echo( '' );
echo( $output );
echo( '' );
echo "End";?>

Hi! Thanks your suggestion didn’t work but tried something else… I used the SheBang (#!) to source the pwsh.exe and then just sudo the script.

I swear I’ve tried it before posting here… but thanks for your time.

My ps1 script:

param (
[string]$vc= “ctelpvc005”,
[string]$vm= “*”
connect-VIserver -Server $vc -User ansible_usr@vsphere.local -Password xxxXXXxxx
Get-DrsClusterGroup -Cluster cl-prd-002 | select Name,Member | where {$_.Member -like $vm}


The working CGI call:

$cmd = “sudo /usr/local/sysadm/root_cmd/drs.ps1 -vc $vc -vm $vm”;


Great to hear this is resolved. Let us know if you have any other questions.