Solved

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

Posted on 2011-02-21
2
771 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now