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 3 months, 1 week ago.

  • Author
    Posts
  • #79337

    Kevyn
    Participant

    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
    Participant

    I found this article helpful for determining error/exception types: https://learn-powershell.net/2015/04/09/quick-hits-finding-exception-types-with-powershell/

  • #79361

    Kevyn
    Participant

    Thanks Matt. I never found that article. 🙂

You must be logged in to reply to this topic.