PowerShell script help required: Confirming computer exists under all domain controllers in a domain?

NHS Chris 0 Reputation points
2024-12-05T09:36:30.7233333+00:00

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  
  }  
} 

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,584 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,706 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Marti Peig 610 Reputation points Microsoft Employee
    2024-12-06T22:41:21.1366667+00:00

    Hi NHS Chris,

    Not sure if you have considered forcing the replication of that particular object to the domain controller you need? Check Sync-ADObject cmdlet, as simple as replicating an AD object between two domain controllers. You may need to identify if the object has been created in the remote DC, and if so force the sync to the local DC so the process can continue.

    I hope it helps.

    1 person found this answer helpful.
    0 comments No comments

  2. Rich Matheisen 47,481 Reputation points
    2024-12-07T03:20:27.9166667+00:00

    When you use the Add-Computer cmdlet, you can specify the DC to be used for that operation by using the -Server parameter. You'll also have to provide the -Credential parameter and value.

    Use that DC in each of the cmdlets in your process. There's no replication delay to worry about if you do that.

    Don't depend on the DC chosen by cmdlet to be the same. In fact, don't depend on any cmdlet to choose the same DC even within the cmdlet!

    You'll find that your problem is quite common when you cede control of DC choice. Even within an AD site, replication is not immediate.

    1 person found this answer helpful.
    0 comments No comments

  3. Marti Peig 610 Reputation points Microsoft Employee
    2024-12-10T16:20:25.5233333+00:00

    Hi NHS Chris,

    If any of the above responses helped resolve your issue, please take a moment to click 'Accept Answer.' This helps guide others with similar questions to the solution faster. Your acknowledgment also supports the community in staying effective and organized!

    Thanks

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.