Capturing Powershell Errors

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 8 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
swatto86
Posts: 26
Last visit: Mon Jul 10, 2023 4:38 am

Capturing Powershell Errors

Post by swatto86 »

EDIT: Apologies I just saw the post below mine to do with handling errors, I think I have the same issue because I am also in a PS-Session to set mailbox permissions.

Good Afternoon,

I have a GUI form that sets mailbox permissions in Office 365. When a mailbox does not exist I can see the powershell error in the Output pane in Powershell Studio 2015 but in my GUI application (when it is packaged) I do not get any error and my code proceeds onwards.

I have a try...catch statement around the mailbox permission command but this does not capture the raw powershell error?

Please could you explain what I am doing wrong?

Thanks,
User avatar
SAPIEN Support Forums
Posts: 945
Last visit: Thu Oct 22, 2015 1:10 pm

Capturing Powershell Errors

Post by SAPIEN Support Forums »

This is an automated post. A real person will respond soon.

Thank you for posting, Swatto86.

Here are some hints to help you get an accurate and complete answer to your question.

Ask in the best forum: If you asked in the wrong forum, just copy your question to the right forum.

Anticipate follow-up questions!

Did you remember to include the following?
  • 1. Product, version and build
    2. 32 or 64 bit product
    3. Operating system, e.g. Windows 7 64 bit.
    4. Attach a screenshot, if applicable
    5. Attach logs, crash reports, etc., in a ZIP file
If not, please take a moment to edit your original post or reply to this one.

*** Make sure you do not post any licensing information ***
User avatar
Cliving
Posts: 57
Last visit: Tue Apr 25, 2023 12:37 pm

Re: Capturing Powershell Errors

Post by Cliving »

"$_" contains the actual terminating error message within a catch block. Depending on the platform option you choose when you built the .exe (whether or not you are suppressing output), you might want to use a message box to display the error to the user.
PowerShell Code
Double-click the code block to select all.
try {
    Get-MailBox -Identity user@domain.com -ErrorAction Stop
}
catch {
    [System.Windows.Forms.MessageBox]::Show("An error occurred performing Get-MailBox. $_")
}
Alternatively, you can use the ErrorVariable parameter.
PowerShell Code
Double-click the code block to select all.
Get-MailBox -Identity user@domain.com -ErrorVariable GMBError
$GMBError
$GMBError contains the error.
This topic is 8 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked