How to determine the exception/error type for use in Catch block

This topic contains 2 replies, has 2 voices, and was last updated by  Kevyn 10 months, 1 week ago.

  • Author
  • #79337


    When an error occurs running a command, how can you determine the error type so that you can catch it in a Try/Catch statement? As a specific example, if you run the commands to connect to Exchange Online twice in the same console, you'll get a warning saying that the proxy creation has been skipped for some commands, followed by the below error:

    Import-PSSession : No command proxies have been created, because all of the requested remote commands would shadow existing local commands. Use the AllowClobber parameter if you
    want to shadow existing local commands.
    At C:\Users\miracles1315\OneDrive\PowerShell\Scripts\Office 365\Connect-O365ExchangeOnline\Connect-O365ExchangeOnline.ps1:129 char:39
    + ... bal:EXOExportedCommands = Import-PSSession $Session -ErrorAction Stop
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidResult: (:) [Import-PSSession], ArgumentException
    + FullyQualifiedErrorId : ErrorNoCommandsImportedBecauseOfSkipping,Microsoft.PowerShell.Commands.ImportPSSessionCommand

    I'm trying to specifically catch for the above error, but I'm also curious how to determine the error type to catch for, including how to determine what parent that inherited type comes from.

  • #79345

    Matt Howard

    I found this article helpful for determining error/exception types:

  • #79361


    Thanks Matt. I never found that article. 🙂

You must be logged in to reply to this topic.