Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Waiting for ID Replication Within Active Directory

Management With PowerShell

Waiting for ID Replication Within Active Directory

by  markdmac  Posted    (Edited  )

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)
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top