Hi all. I'm new to PowerShell coding and require assistance if anyone can help me?
I work with a domain that has 5 domain controllers. 4 are local, 1 is remote and there is a 20 minute replication delay between the locals and remote. This cannot be changed by me.
We build a lot of thin clients in a scripted sequence involving multiple reboots taking around 10 minutes. The final part of this sequence adds the client computer object into AD groups and moves it to a different OU.
The problem is this sequence doesn't always complete because the computer object was initially created on the remote AD server, while the latter update is being attempted on a local AD server whilst replication is yet to occur (or vice versa!)
As mentioned, I cannot adjust or change the replication times so I need to change the final sequence so that is searches all ad servers for the computer object and picks any servers that report the object exists, rather than simply quit when it doesn't exist.
I found this great reply by Andreas Baumgarten which I thought was the answer: https://learn.microsoft.com/en-us/answers/questions/1153640
..and implemented it, but realised it wasn't working like I thought when it too fell over after replication hadn't happened.
Can someone please tell me how to fix the script so it searches all available AD controllers, and picks any of them that contains the computer object to continue with the changes? The goal is to no longer get this issue where slow replication trips up the sequence; the object will always exist on at least one server, it's just making sure it's picked over the others that don't yet contain the object.
Code below
Get-ADDomainController | ForEach-Object {
try {
$compObj = Get-AdComputer -Identity $env:computername -Server $_.Name -ErrorAction SilentlyContinue
if ($compObj) {
Set-ADComputer $env:computername -Description "T655 (Build Version 1.3)" -Server $_
Add-ADGroupMember -Identity PatchMgr_ThinClient_Excluded -Members $env:computername$ -Server $_
Get-ADComputer $env:computername | Move-ADObject -TargetPath '<our domain path>'
}
}
catch {
Write-Host "($_)" -ForegroundColor Red
Pause
}
}