none
PowerShell - Message if the command run successfully RRS feed

  • Question

  • Hello,

    I made a script in powershell with windows form where I put an active directory group in one textbox and the user login in another textbox and when clicking the button, the command runs and includes the user as a member of the group.

    So far all ok, but I have no information if the command worked or if error.

    How do I show in a message if access has been released or if there were any errors in the command?

    I know it's from the class: [System.Windows.Forms.MessageBox] :: Show ("My message")

    But how ?


    See below:

    $buttonGrant_Click={
            $textboxResultado.Lines = Add-ADGroupMember -Identity $textboxGroup.Text -Members $textboxLogin.Text
        }

    Thank you very much in advance

    Saturday, November 2, 2019 8:17 PM

All replies

  • Use Try/Catch to trap any errors.

    help about_try_catch


    \_(ツ)_/

    Saturday, November 2, 2019 9:06 PM
  • You can also use the special variable $? to check if there was an error during execution of the previous command

    if there was an error, then the variable $? will contain $false


    my blog: http://shserg.ru/

    Wednesday, November 6, 2019 6:42 AM
  • You can also use the special variable $? to check if there was an error during execution of the previous command

    if there was an error, then the variable $? will contain $false


    my blog: http://shserg.ru/

    Won't work correctly in this case. Try/Catch was added to overcome limitations in that old error trapping method which was used in PS v1 and maintained for backward compatibility


    \_(ツ)_/

    Wednesday, November 6, 2019 6:45 AM