?
Solved

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

Posted on 2011-02-21
2
Medium Priority
?
778 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 2000 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

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…
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

771 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