need help

Ask your PowerShell-related questions, including questions on cmdlet development!
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.
Posts: 1
Last visit: Sun Aug 07, 2022 12:59 am

need help

Post by TheMooxi »

I need to put a msgbox indicating if the command below was executed successfully or not

#$GroupTest = $textboxCheckGroup.Text
$textboxResultado.Lines = Add-ADGroupMember -Identity $textboxGroup.Text -Members $textboxLogin.Text

If the command execute successfully shows the msg in the popup: 'Access Released'
If the user already has group access, show the msg in the popup: 'User already has group access'
If the command does not execute successfully, displays the msg in the popup: 'Command not executed, check data entered'

Can you help me?
Posts: 15359
Last visit: Tue Jan 31, 2023 2:55 pm
Answers: 27
Has voted: 4 times
Been upvoted: 31 times

Re: need help

Post by jvierra »

If the command doesn't execute correctly, it will throw an exception. You will have to catch the exception and decide which message you want to display.

Start by reviewing the documentation on exception handling in PowerShell. Also review the AD command for how it generates exceptions.

Start with help:
help about_try_catch_finally