powershell How do I capture a return code from a script I run on a remote computer

Bruce Taylor 1 Reputation point
2021-06-18T17:02:46.727+00:00

powershell How do I capture a return code from a script I run on a remote computer

here is my script
Param(
[Parameter(Position=1)]
[string]$IPaddress,

[Parameter(Position=2)]
[string]$Clustername

)

$creds=import-clixml -Path /tmp/tent.xml
Invoke-Command -ComputerName $IPaddress -Authentication Negotiate -Credential $creds -FilePath \tmp\check_cluster_node.ps1 -ArgumentList $Clustername

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,418 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Andreas Baumgarten 98,631 Reputation points MVP
    2021-06-18T17:14:13.037+00:00

    Hi @Bruce Taylor ,

    maybe this helps to get started:

    add $LASTEXITCODE at the end of check_cluster_node.ps1

    than modify the last line of the script you posted above:

    $result = Invoke-Command -ComputerName $IPaddress -Authentication Negotiate -Credential $creds -FilePath \tmp\check_cluster_node.ps1 -ArgumentList $Clustername  
    $result  
    

    The variable $result should contain the output of $LASTEXITCODE.

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


  2. Andreas Baumgarten 98,631 Reputation points MVP
    2021-06-18T17:50:37.857+00:00

    Hi @Bruce Taylor ,

    could you please try this:

    $Clustername = "NameOfCluster"  
    $ServerName = "NameofServer"  
    $creds=import-clixml -Path /tmp/tent.xml  
    $S = New-PSSession -ComputerName $ServerName -Credential  $cred  
    $result = Invoke-Command -Session $S -FilePath \tmp\check_cluster_node.ps1 -ArgumentList $Clustername  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  3. Bruce Taylor 1 Reputation point
    2021-06-18T18:05:11.837+00:00

    This is what you wanted correct
    $Clustername = UHSISMONCPRB
    $ServerName = 172.20.231.69
    $S = New-PSSession -ComputerName $ServerName
    $creds=import-clixml -Path /tmp/tent.xml
    $result = Invoke-Command -Session $S $IPaddress -Authentication Negotiate -Credential $creds -FilePath \tmp\check_cluster_node.ps1 -ArgumentList $Clustername
    $result


  4. Rich Matheisen 45,186 Reputation points
    2021-06-18T19:10:54.723+00:00

    Can we see the script that runs on the remote computer?

    The Invoke-Command will return all data sent to the Success stream during the execution of the script on the remote machine. "Echo" is an alias for the "Write-Output" cmdlet so that data should have been returned.

    I ran a simple test:
    $creds=get-credential
    $whathappened = Invoke-Command -ComputerName srv02 -Authentication Negotiate -Credential $creds -FilePath c:\junk\untitled-1.ps1

    of this script:
    "Hello, there!"
    $return = 0
    $return

    and received this as the result on the local machine:
    PS C:\Users\richm> $whathappened

    Hello, there!
    0