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 about 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 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 explain what code I need 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 so I 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 used over the others that don't yet contain the object.
So goals summarised for the script
- Search all available AD servers for client computer object
- Automatically pick any one server from the list that reported the object exists/or simply pick the first server which reports it exists.
- Apply the groups to the selected server/computer 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 ''
}
}
catch {
Write-Host "($\_)" -ForegroundColor Red
Pause
}
}