How do I capture errors and assign to variable?

This topic contains 6 replies, has 4 voices, and was last updated by Profile photo of Robert Westerlund Robert Westerlund 2 years, 7 months ago.

  • Author
    Posts
  • #14839
    Profile photo of GS
    GS
    Participant

    Hello,

    Response from Receive-Job contains some errors and I want to capture those in variable for some reason those are missing from variable but do output to screen when required. Example is below. I'd like $resp variable to contain error message as well, not only "Server1 processing finished"

    PS C:\Users\admin>> $Sender | Receive-Job -Keep
    The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
    + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
    + PSComputerName : localhost

    Server1 Processing finished

    PS C:\Users\admin>> $resp = ($Sender | Receive-Job -Keep)
    The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
    + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
    + PSComputerName : localhost

    PS C:\Users\admin>> $resp
    Server1 Processing finished

  • #14840
    Profile photo of Don Jones
    Don Jones
    Keymaster

    use Get-Job to get the job object, and pipe it to Get-Member. You should see properties of the job object that provide access to the various pipelines.

    Keep in mind that in PowerShell, error messages are never part of the "main" output – and Remoting only captures the "main" output. Errors, warnings, and so on have their own pipelines, or channels. They're not always capturable with Remoting.

  • #14842
    Profile photo of GS
    GS
    Participant

    Thanks.
    For anybody having the same issue following will capture error stream as well

    ($Sender | Receive-Job 2>&1)

  • #14843
    Profile photo of Don Jones
    Don Jones
    Keymaster

    And just to clarify what that's doing: It's taking the error pipeline (#2) and redirecting it into the main output pipeline (#1), mingling the two.

  • #14848
    Profile photo of GS
    GS
    Participant

    For some reason does not work for me

    PS C:\Users\admin>> Receive-Job 661 -Keep -ErrorVariable $RemoteErr
    The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
    + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
    + PSComputerName : localhost

    Server1 finished

    PS C:\Users\gsuvalian>> $RemoteErr -eq $null
    True

  • #14846
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    ErrorVariable and ErrorAction seem to work with Receive-Job just like any other cmdlet (though this test was running a local job, not remote. I don't have another computer up on my home network at the moment for a full test.)

    $job = Start-Job -ScriptBlock { Write-Error "This is an error."; Write-Output "This is output." }
    $null = $job | Wait-Job
    $output = $job | Receive-Job -Keep -ErrorAction SilentlyContinue -ErrorVariable remoteErrors
    
    $remoteErrors.Count
    $remoteErrors[0]
    
  • #14857
    Profile photo of Robert Westerlund
    Robert Westerlund
    Participant

    You should remove the '$' character from the '-ErrorVariable' parameter. Your code should probably be:

    Receive-Job -ID 661 -Keep -ErrorVariable RemoteErr
    $RemoteErr -eq $null

    You can read more about it if you run

    Get-Help about_CommonParameters

You must be logged in to reply to this topic.