How to excute a Shell Script from PowerShell Script as a part of Automation

JyotiPrakash Mallick 1 Reputation point
2022-01-06T12:32:26.827+00:00

Context:

  • Want to execute a Shell-Script from a PowerShell script.

Steps:

  1. Connect to the VM through PowerShell
  2. Execute a Shell script.

PowerShell Script:

function remoteConnectEngine($secUsername, $VmPassword, $remoteMachine) {
    $password = ConvertTo-SecureString $VmPassword -AsPlainText -Force;
    $pscredential = New-Object -TypeName System.Management.Automation.PSCredential("$secUsername", $password);

    removeExistingSession
    $cmd = "bash ./datadisk01/PerfAutomation/startJMeterServer.sh"
    foreach ($rm in $remoteMachine) {
         $currentSessionID = New-SSHSession -ComputerName $rm -Credential $pscredential
         Invoke-SSHCommand -SessionId $currentSessionID.SessionId -Command $cmd
    }
}
function removeExistingSession {

    $remove = Get-SSHSession
    if ($null -eq $remove -or $remove -eq 0) {
        Write-Host "There is no active session opened previously !!!"
    }
    else {
        Write-Host "There are few existing session opened with the previous connection, removing them!!!"
        foreach ($existingSessionID in $remove.SessionId){
            $removeTheSession = Remove-SSHSession -SessionId $existingSessionID
                if($removeTheSession) {
                    Write-Host "Existing Session ID" $existingSessionID "has been removed successfully!!!"
                    }
                else { 
                    Write-Host "There is a problem with removing the existing session with ID" $removeTheSession
                }
        }
    }

    }

function connectionDetails($targetMachine) {
    $LoadAgentUserName       = "TestUser";
    $LoadAgentVMPassword     = "<Password>";
    $vm_Machine              = $targetMachine;
    remoteConnectEngine $LoadAgentUserName $LoadAgentVMPassword $vm_Machine
}

function startJMeterServer {
    $loadAgentMachine = 'npd-test-app03'
    connectionDetails $loadAgentMachine

}

startJMeterServer

Shell Script:

#!/bin/bash  

startJMeterServer () {
 echo "Change current directory to the JMeter !!!";
 cd /datadisk01/PerformanceTool/apache-jmeter-5.3/bin || exit;
 echo "Validating, if there is any JMeter server is running in the current machine?";
 if [[ -z $(ps -ef |grep "jmeter-server"|grep -v grep) ]]
 then 
 echo "There is no JMeter process is running!!!";
 else
 java_processID=$(ps -e -H -o pid,comm | awk '$2 ~ /jmeter-server/ { print $1}')
 echo "There is a already java process running !!!";
 if [[ -z "$java_processID" ]]
 then
 echo "No process is running with signature JMeter";
 else
 echo "Process Id for running JMeter process is:""$java_processID";
 echo "Killing the existing JMeter Server process !!!"
 #ps -e -H -o pid,comm | awk '$2 ~ /test-server/ { print $1}' | xargs kill -9
 if [ $? -eq 0 ]; then
    echo "JMeter Server killed successfully";
 else
    echo "Error while terminating the JMeter server process";
 fi
 fi

 fi
 #ls -lrt

}

startJMeterServer

I am getting:
Host : <>
Output : {}
ExitStatus : 127

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2022-01-06T14:11:25.787+00:00

    Host : <>
    Output : {}
    ExitStatus : 127

    An internet search says that ExitStatus 127 is "command not found".

    Instead of running bash, can you run a dir or ls command to verify that you can see the script file.

    0 comments No comments

  2. Limitless Technology 39,916 Reputation points
    2022-01-11T08:23:06.547+00:00

    Hi there,

    Value 127 is returned by /bin/sh when the given command is not found within your PATH system variable and it is not a built-in shell command. In other words, the system doesn't understand your command, because it doesn't know where to find the binary you're trying to call.

    Here is a thread as well which discusses the same issue and you can try out some troubleshooting steps from this and see if that helps you to sort the Issue.
    https://powerusers.microsoft.com/t5/Building-Flows/Execute-Power-Shell-scripts-through-Power-Automate-flows/td-p/731472


    --If the reply is helpful, please Upvote and Accept it as an answer--

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.