none
How to catch a PowerShell remoting exception RRS feed

  • Question

  • Hi,

    I need to run a PowerShell script on a number of computers. Doing some sampling, I discovered that remoting is not necessarily running on all of the computers and causes the script to fail. Remoting should not be enabled on computers where it fails though. What I am trying to do is to get the script to log what happened and then terminate gracefully using Try-Catch.

    I have tried catching PSRemotingTransportException and PSSessionStateBroken exceptions but an error is being thrown in both cases.

    try {
    invoke-command -ComputerName DISPATCH -ScriptBlock {Get-ChildItem C:\Users -Exclude admin*,default*,public,temp*}
    }
    
    catch [PSRemotingTransportException] {
      Write-Host "Remoting disabled."
    }

    This is the error being thrown:

    [DISPATCH] Connecting to remote server DISPATCH failed with the following error message : The client cannot connect to the destination specified in the request. Verify that the
    service on the destination 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 is 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_Troubleshooting Help topic.
        + CategoryInfo          : OpenError: (DISPATCH:String) [], PSRemotingTransportException
        + FullyQualifiedErrorId : CannotConnect,PSSessionStateBroken

    Thank you!

    Rob

    Tuesday, November 5, 2019 10:45 PM

Answers

  • Just place your file output in the "Catch" block.

    You also need to use ErrorAction Stop

    To use PowerShell it is necessary to read the instructions so rad about how to use all commands and the try/catch.

    help invoke-command -online

    help about_try_catch

    help help


    \_(ツ)_/

    • Marked as answer by robwm1 Wednesday, November 6, 2019 3:19 AM
    Wednesday, November 6, 2019 12:01 AM

All replies

  • You need to read the message and follow the instructions given. Have you done that?


    \_(ツ)_/

    Tuesday, November 5, 2019 11:16 PM
  • The instructions are suggesting some services are not running. I did state that if WinRM service is not running, then it should not be enabled. I would prefer to just catch the error, output something to a log and quit.

    If this can't be done then I'll just have to live with the results I'm seeing.

    Tuesday, November 5, 2019 11:31 PM
  • Just output the error to your log. Why is that an issue?


    \_(ツ)_/

    Tuesday, November 5, 2019 11:35 PM
  • I haven't found a way to do that so far. The script above terminates with the error. That is why I posted here.
    Tuesday, November 5, 2019 11:37 PM
  • Just place your file output in the "Catch" block.

    You also need to use ErrorAction Stop

    To use PowerShell it is necessary to read the instructions so rad about how to use all commands and the try/catch.

    help invoke-command -online

    help about_try_catch

    help help


    \_(ツ)_/

    • Marked as answer by robwm1 Wednesday, November 6, 2019 3:19 AM
    Wednesday, November 6, 2019 12:01 AM