Calling powershell script from php

Welcome Forums General PowerShell Q&A Calling powershell script from php

This topic contains 5 replies, has 2 voices, and was last updated by

 
Participant
2 weeks, 2 days ago.

  • Author
    Posts
  • #172972

    Participant
    Topics: 1
    Replies: 2
    Points: 2
    Rank: Member

    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.

    < ?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";
    ?>

  • #173089

    Participant
    Topics: 0
    Replies: 100
    Points: 363
    Helping Hand
    Rank: Contributor

    Typer10039,

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

    https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-linux?view=powershell-6

  • #173119

    Participant
    Topics: 1
    Replies: 2
    Points: 2
    Rank: Member

    [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.

     

     

  • #173281

    Participant
    Topics: 0
    Replies: 100
    Points: 363
    Helping Hand
    Rank: Contributor

    Typer,

     

    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";?>
  • #174589

    Participant
    Topics: 1
    Replies: 2
    Points: 2
    Rank: Member

    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:

    #!/bin/pwsh
    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";

  • #174622

    Participant
    Topics: 0
    Replies: 100
    Points: 363
    Helping Hand
    Rank: Contributor

    Typer,

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

     

You must be logged in to reply to this topic.