Simple do..while – check for user account existence.

Welcome Forums General PowerShell Q&A Simple do..while – check for user account existence.

Viewing 2 reply threads
  • Author
    Posts
    • #242348
      Participant
      Topics: 1
      Replies: 1
      Points: 19
      Rank: Member
      Hello Powershell.org Community,
      First time poster, semi long term reader. Just need help with what I think will be straight forward for the professionals out there.
      I’m trying to generate a username based on the first initials of their first and last name, and then adding ‘user’ on the end, ie ‘SMUser’. However sometimes that username is taken, so I would like to take the second character of the last name, and add that to the user name ie ‘SMcUser’, this is easy enough to do, but I’m trying to take it further and ask it to check AD for an existing account, if it finds it, it extends the username even further by adding another character.  I have never used do..while before, and I can see the problem, however I’m not sure if I’m going about it the right way.
      Any help appreciated, oh and the silentlycontinue looks to be the main issue… it doesn’t continue.
      $FirstName = 'Steve'
      $LastName = 'McQueen'
      do {
          $x = 0
          $NewAlias = $FirstName[0], $LastName.Substring(0, $x), 'User' -join ""
          $UserAlias = $NewAlias.ToLower()
          write-output $UserAlias
          $x = $x++
      } while ([bool](Get-ADUser -Identity $UserAlias -ErrorAction SilentlyContinue) -eq $true)
      • This topic was modified 2 months, 1 week ago by marcosis29.
      • This topic was modified 2 months, 1 week ago by marcosis29.
    • #242357
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      Is it just a typo here in your post? … the variable you use in your Get-ADUser query is different to the variable inside your while loop. $UserAlias vs. $UserAlia

    • #242375
      Participant
      Topics: 1
      Replies: 1
      Points: 19
      Rank: Member

      Thanks, edited 🙂 That was me fixing up variables for privacy reasons.

Viewing 2 reply threads
  • You must be logged in to reply to this topic.