×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

PowerShell (Microsoft) FAQ

Management With PowerShell

Waiting for ID Replication Within Active Directory by markdmac
Posted: 18 Sep 14


Take Action After ID Replicates
I've been working on a GUI based PowerShell script that simplifies user creation at my company. The script locates the least used Exchange database, creates a mailbox, reads from an AD template and joins the user to a number of groups depending on the selected template, applies additional group memberships and creates a Lync account. There is a lot going on and I was struggling with the code completing because of a slight delay in the user ID being visible to the script despite specifying the domain controller to create the ID on. I determined I needed to add in some code that simply waits until the ID can be located. Below is the simple wrapper code that searches a given Domain Controller for a given username and waits until it is found before performing the next actions. I thought this would be useful as a snippet for others that might be writing similar tools in PowerShell.

CODE

$DC = "TekTipsDC01"
$Stoploop = $false
$testUser = $null
$alias = "UsernameToSearchFor"
do {
    try {
        write-host "Waiting for ID"
        $testUser = Get-ADUser "$alias" -Server "$DC"
        $Stoploop = $true
        }
    catch {
        if ($testUser -ne $null){
            Write-Host "ID Found - Code to Execute Goes In This Section"
            $Stoploop = $true
        }
     else {
            Write-Host "Could not find, retrying in 5 seconds..."
            Start-Sleep -Seconds 5
        }
    }
}
While ($Stoploop -eq $false) 

Back to PowerShell (Microsoft) FAQ Index
Back to PowerShell (Microsoft) Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close