Quantcast
Viewing all articles
Browse latest Browse all 311

PowerShell • Need to stop and display error if user exists

I have a form I created that creates user objects in AD. As part of it I added a button to check if the proposed samaccountname already exists, if it does it displays it in a text box. However, as this relies on the tech creating the accounts to actually check rather than just hitting go we have seen new users arrive without an account because someone else is already using that ID.

So, I want to add code to make it check the proposed user ID when the button is pressed to create the account and, if it exists display a message in the same text box used to display the results if they check for the account and stop the script running.
Problem is, I can't seem to get it to do either, it doesn't display anything and carries on trying to create something that already exists then sends a completion email saying it was a good boy and asking for a belly rub (not a verbatim transcript of the email of course)

After reading several forums and using code found for similar but not exact scenarios I have come up with this so far

Code:

        if ((Get-ADUser -Filter "sAMAccountName -eq '$($User_ID_Input_Box.Text)'")) {    Out-String $User_ID_Check_Output.Focus "User already exists"}
This does not trigger any errors when I run the entire script but neither does it display anything in the text box despite using the samaccountname of a test account I know exists, I have not included the stop or else here as they are not the primary concern right now, even without them in the script it does exactly the same thing, the first step is to get it to display the fact the ID is in use.

What am I missing/how do I get it to do what I need?

Statistics: Posted by GarethDavies — Tue Mar 19, 2024 1:52 pm — Replies 2 — Views 99



Viewing all articles
Browse latest Browse all 311

Trending Articles