How to execute a powershell script which will make SSH connection using a windows mid server

Naresh Kumar G2
Tera Contributor

I have a requirement to execute a powershell script which will execute on a MID server hosted on a windows server that in turn makes a powershell ssh connection.

The script I am trying to execute is given below and it is passed as a variable to the Powershell step,

 

function Get-DriveSpace {
        $CommandDrive = @"
echo "$Password" | sudo -S df -h
"@

        try {
            $SessionID = New-SSHSession -ComputerName $ComputerName -Credential $Credentials -AcceptKey -ErrorAction Stop
            $Result = Invoke-SSHCommand -Index $SessionID.Sessionid -Command $CommandDrive
            $Result.Output
            CheckDiskSpace
            Remove-SSHSession -Index $SessionID.Sessionid
        }
        catch {
            $_.Exception.Message
        }

    }
    Get-DriveSpace
 
Command script:

#****** Format to access Input Parameters *******
# To use variable, prefix with '$'. eg. $message
# Read more https://docs.servicenow.com/?context=CSHelp:IntegrationHub-PowerShell-Step

#****** Reserved variables available when remoting type is Run on MID
# $computer Host IP resolved from Connection alias
# $cred Credential Object. Credential resolved via connection alias or credential alias. This can be used in conjunction with any cmd-let which support credential parameter eg. New-PSSession -credential $cred
# $log_info mid property "mid.property.powershell.log_info" set on instance to enable debugging. So it's a flag available to act on and add any verbose logging if they want to in their script

$ErrorActionPreference = "Stop"

try{


$Session = New-PSSession -ComputerName $Servername -Credential $cred

$Result = Invoke-Command -Session $Session -ScriptBlock {
iex $Using:pscript
}

Write-Host $Result

}

catch{
[PSCustomObject]@{
result = $_.exception.message
} | ConvertTo-Json
}

 What is the configuration I should make so that PowerShell Step works fine.
Configuration screenshot attached for reference.
 
 
0 REPLIES 0