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

Welcome Forums General PowerShell Q&A 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

 
Participant
1 year, 1 month ago.

  • Author
    Posts
  • #79337

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 1
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    Thanks Matt. I never found that article. 🙂

The topic ‘How to determine the exception/error type for use in Catch block’ is closed to new replies.