Solved

PowerShell: Debug Help with User Name Check Using Get-QADUser

Posted on 2011-02-21
2
777 Views
Last Modified: 2012-05-11
Ok,

So what I'm attempting to do is take a user ID stored in $strSamAccountName and check to see if it exists in AD.  

If it does, then a result will be stored in $CheckUser.

If it does not, then $CheckUser is $Null, since the Quest Get-QADUser will not find anything.

I also have another variable setup that is for a separate option user ID called $strOptionUID.  (This is part of a new user script that uses an Excel form.  The requestor can add an option user logon ID if they know there is someone else with the same name in Active Directory.)

Anyway, this code causes me some issues.  The output of this code results in the new user ID being "userxyz22;" if the user id exists it's always got a 22 after it.

What I want to do is change the provided user ID to userxyz2, userxyz3, etc. if the ID is in AD.

$Counter = 2
      $FoundName = $false
      $CheckUser = $null
      $CheckUser = Get-QADUser $strSamAccountName
      if (($CheckUser -ne $null) -and ($strOptionalUID -eq $null))
      {
            :UpdateUID do
            {
            $samAccountTemp = $null
            $samAccountTemp = $strSamAccountName + "$Counter"
            $CheckUser = Get-QADUser $samAccountTemp
            if ($CheckUser -eq $null) {$strSamAccountName = $samAccountTemp + "$Counter";$strUserPrincipalName = $strSamAccountName + "@company.com";$strMailAddress = $strUserPrincipalName;$strMailAlias = $strSamAccountName;$FoundName = $True}
            $Counter = $Counter + 1
            }while($FoundName -ne $true)
      }

So, what did I do wrong?

GB
0
Comment
Question by:gerhardub
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 71

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 34949573
It does it because of this:
$samAccountTemp = $null
$samAccountTemp = $strSamAccountName + "$Counter

if ($CheckUser -eq $null) { $strSamAccountName = $samAccountTemp + "$Counter" ...

Open in new window

You combine $samAccountTemp with $Counter twice. Otherwise it would return userxyz2 instead of 22.

Anyway can't we simplify that a bit?
$BaseUsername = $strSamAccountName
If ($strOptionalUID) { $BaseUsername = $strOptionalUID }

$Counter = 1
Do {
  $Username = "$BaseUsername$Counter"
  $Counter++
} Until (!(Get-QADUser -SamAccountName $Username))

Write-Host "Username: $Username"
Write-Host "Alias: $Username"
Write-Host "UPN: $Username@company.com"
Write-Host "Mail: $Username@company.com"

Open in new window

Chris
0
 
LVL 1

Author Closing Comment

by:gerhardub
ID: 34951138
Yes, we could simplify it, but I want anyone who comes after me to easily be able to figure out what's going on.

Thank you sir!

GB
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question