Solved

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

Posted on 2011-02-21
2
773 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
2 Comments
 
LVL 70

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

832 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