how to catch difference in samaccountName

Welcome Forums General PowerShell Q&A how to catch difference in samaccountName

Viewing 5 reply threads
  • Author
    Posts
    • #226308
      Participant
      Topics: 44
      Replies: 97
      Points: 605
      Rank: Major Contributor

      Hi

      In AD by preference the Users Email address isĀ  SamAccountName + @mydomain.com.
      I recently noticed that there are differences here where the SamAccountName is not following this standard(don’t ask me why)

      in my offboarding script it can happen that the $user.samAccountName returns an error because the standard convention as mentioned above has not been respected, or the user has asked to change his name From Paulus into Paul.

       

      my script looks as follows

      in the CSV I have 2 headers SamAccountName and Email
      example
      SamAccountName, Email

      Paul,[email protected]

      userPrincipalName = [email protected]
      SamAccounName = Paulus

      What I would like to achieve is that if $user.SamAccountName fails that I will take the required steps to Modify the samAccountName Based upon the $User.email

      this code I use to capture the difference in samaccountName

      and would like to integrate this in the below for each I only don’t know what the best way is using the IF statement or a select case senario?

      thanks for your input

      Paul

      • This topic was modified 7 months ago by acer460527.
      • This topic was modified 7 months ago by acer460527.
    • #226371
      Senior Moderator
      Topics: 9
      Replies: 1422
      Points: 5,290
      Helping Hand
      Rank: Community MVP

      you may use Try catch block as it throws error and may be in catch block you can read the error continue based on the error.

    • #226413
      Participant
      Topics: 17
      Replies: 1872
      Points: 3,640
      Helping Hand
      Rank: Community Hero

      Another option is to just search for both values, but it would be possible to return more than one user:

    • #226584
      Participant
      Topics: 44
      Replies: 97
      Points: 605
      Rank: Major Contributor

      Thanks for your reaction

       

      • This reply was modified 7 months ago by acer460527.
    • #226605
      Participant
      Topics: 44
      Replies: 97
      Points: 605
      Rank: Major Contributor

      I now face the following issue that when I want to update the SamAccountName with the New SamAccountName
      in this example it works

      error that I now get is

      How can I make sure that the $user.SamaccountName is populated with “Paulus” instead of the value found in the CSV file?
      Do I need to update the CSV file first or is there a different way other then what I tried above?

      • This reply was modified 7 months ago by acer460527.
      • This reply was modified 7 months ago by acer460527.
    • #227005
      Participant
      Topics: 44
      Replies: 97
      Points: 605
      Rank: Major Contributor

      hi

      I discovered that when I use Bob’s solution it works while directly working on the AD server with PSSession it’s not any suggestions?

      Paul

Viewing 5 reply threads
  • The topic ‘how to catch difference in samaccountName’ is closed to new replies.