Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
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…

730 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