Try catch not working

This topic contains 3 replies, has 2 voices, and was last updated by  Norm Long 1 week, 6 days ago.

  • Author
    Posts
  • #98014

    Norm Long
    Participant

    Good afternoon!
    Running the following script:

    $output1 = foreach ($name in $names) {
    try {
    Invoke-Command -ComputerName $name -ScriptBlock {$PSVersionTable.psversion} -ErrorAction Silentlycontinue -ErrorVariable exception | Out-File -FilePath 'D:\Powershell_temp\psversion.txt' -Append
    } catch {
    $exception = $_.Exception.Message
    #$failedItem = $_.Exception.ItemName
    Out-File -FilePath 'D:\Powershell_temp\psversion_message.txt' -Append -InputObject $exception
    #Out-File -FilePath 'D:\Powershell_temp\psversion_itemname.txt' -Append -InputObject $failedItem

    }
    }

    The script completes but there is nothing in the psversion_message.txt file. What am I missing?

    Thank you for your input.

    Norm

  • #98031

    Norm Long
    Participant

    Hope the following will be of some help in solving this opportunity:

    [4:32 PM]ndl@FWTS012:Powershell_Test $ clear-content -Path 'D:\Powershell_temp\psversion.txt'
    $output1 = foreach ($name in $names) {
    try {
    Invoke-Command -ComputerName $name -ScriptBlock {$PSVersionTable.psversion} -ErrorAction Silentlycontinue -errorvariable exception | Out-File -FilePath 'D:\Powershell_temp\psversion.txt' -Append
    } catch [System.Management.Automation.Remoting.PSRemotingTransportException] {
    $_.Exception.Message | out-file -FilePath `D:\PowerShell_temp\foo.out` -Append
    Write-Host "Message: [$($_.Exception.Message)"] -ForegroundColor Red -BackgroundColor DarkBlue
    $exception = $_.Exception.Message
    Out-File -FilePath 'D:\Powershell_temp\psversion_message.txt' -Append -InputObject $exception

    }
    }

    [4:40 PM]ndl@FWTS012:Powershell_Test $ $error[0].Exception.Message
    Connecting to remote server TESTVM160418 failed with the following error message : The client cannot connect to the destination specified in the request. Verify that the service on the des
    tination is running and is accepting requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If the destination i
    s the WinRM service, run the following command on the destination to analyze and configure the WinRM service: "winrm quickconfig". For more information, see the about_Remote_Troubleshootin
    g Help topic.

    [4:41 PM]ndl@FWTS012:Powershell_Test $ $error[0].Exception | Get-Member

    TypeName: System.Management.Automation.Remoting.PSRemotingTransportException

    Name MemberType Definition
    —- ———- ———-
    Equals Method bool Equals(System.Object obj), bool _Exception.Equals(System.Object obj)
    GetBaseException Method System.Exception GetBaseException(), System.Exception _Exception.GetBaseException()
    GetHashCode Method int GetHashCode(), int _Exception.GetHashCode()
    GetObjectData Method void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context), void ISerializabl...
    GetType Method type GetType(), type _Exception.GetType()
    ToString Method string ToString(), string _Exception.ToString()
    Data Property System.Collections.IDictionary Data {get;}
    ErrorCode Property int ErrorCode {get;set;}
    ErrorRecord Property System.Management.Automation.ErrorRecord ErrorRecord {get;}
    HelpLink Property string HelpLink {get;set;}
    HResult Property int HResult {get;set;}
    InnerException Property System.Exception InnerException {get;}
    Message Property string Message {get;}
    Source Property string Source {get;set;}
    StackTrace Property string StackTrace {get;}
    TargetSite Property System.Reflection.MethodBase TargetSite {get;}
    TransportMessage Property string TransportMessage {get;set;}
    WasThrownFromThrowStatement Property bool WasThrownFromThrowStatement {get;set;}

    Thank you in advance for your input!

    Norm

  • #98034

    Joel Sallow
    Participant

    This is your issue:

    -ErrorAction Silentlycontinue

    Basically, you're telling PS to completely ignore all errors that crop up. This effectively negates the try block for the command you apply it to and renders it useless; it can't catch a suppressed error.

    By default, some errors in PS are terminating, and others are not. To consistently ensure that any errors trigger your catch block, you must instead use -ErrorAction Stop 🙂

    • #98142

      Norm Long
      Participant

      Thank you Joel, that was the issue.

      Thanks again

      Norm

You must be logged in to reply to this topic.