Move and Rename Computer Object Despite Slow DC Syncronization

%3CLINGO-SUB%20id%3D%22lingo-sub-1949458%22%20slang%3D%22en-US%22%3EMove%20and%20Rename%20Computer%20Object%20Despite%20Slow%20DC%20Syncronization%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1949458%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAt%20my%20workplace%2C%20I%20configure%20properties%20of%20computer%20objects%20in%20my%20domain%20using%20PowerShell.%20Specifically%2C%20I%20change%20the%20name%20of%20a%20computer%2C%20then%20move%20it%20to%20its%20respective%20organizational%20unit.%20Seems%20simple%20enough%2C%20right%3F%20Unfortunately%2C%20my%20Powershell%20script%20only%20work%20intermittently%20due%20to%20domain%20controllers%20not%20syncing%20fast%20enough%3F%20Allow%20me%20to%20explain%20exactly%20what%20happens%20by%20showing%20commands.%20Note%20that%20each%20command%20works%20fine%20when%20run%20separately.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24computername%20%3D%20'oldname'%3C%2FP%3E%3CP%3E%24newcomputername%20%3D%20'newname'%3C%2FP%3E%3CP%3E%24path%20%3D%20'someworkingpath'%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3ERename-Computer%3C%2FSTRONG%3E%20-ComputerName%20%24computername%20-NewName%20%24newcomputername%20-DomainCredential%20%24credential%20-Restart%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EGet-ADComputer%3C%2FSTRONG%3E%20-ComputerName%20%24newcomputername%20%7C%20Move-ADObject%20-TargetPath%20%24Path%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAfter%20Rename-Computer%20is%20executed%2C%20the%20name%20is%20changed%2C%20but%20changes%20are%20slow%20to%20synchronize%20across%20the%2010%20other%20domain%20controllers%20in%20my%20domain.%20Then%20once%20Get-ADComputer%20is%20executed%2C%20the%20newcomputername%20cannot%20be%20located.%20Changing%20the%20Get-ADComputer%20command%20to%20get%20the%20%24oldcomputername%20causes%20strange%20behavior.%20Given%20that%20I%20do%20not%20have%20access%20to%20change%20domain%20controller%20settings%20myself%2C%20is%20there%20anything%20I%20can%20do%20to%20make%20these%20commands%20run%20one%20after%20the%20other%20despite%20having%20domain%20controllers%20that%20are%20slow%20to%20sync%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1949458%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Edomain%20controller%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Occasional Visitor

 

 

At my workplace, I configure properties of computer objects in my domain using PowerShell. Specifically, I change the name of a computer, then move it to its respective organizational unit. Seems simple enough, right? Unfortunately, my Powershell script only works intermittently due to domain controllers not syncing fast enough? Allow me to explain exactly what happens by showing commands. Note that each command works fine when run separately.

 

$computername = 'oldname'

$newcomputername = 'newname'

$path = 'someworkingpath'

 

Rename-Computer -ComputerName $computername -NewName $newcomputername -DomainCredential $credential -Restart

 

Get-ADComputer -ComputerName $newcomputername | Move-ADObject -TargetPath $Path

 

After Rename-Computer is executed, the name is changed, but changes are slow to synchronize across the 10 other domain controllers in my domain. Then once Get-ADComputer is executed, the newcomputername cannot be located. Changing the Get-ADComputer command to get the $oldcomputername causes strange behavior. Given that I do not have access to change domain controller settings myself, is there anything I can do to make these commands run one after the other despite having domain controllers that are slow to sync?

 

1 Reply

@ssstier I have come across similar behaviour in a few of my scripts in multi domain controller environments with delayed replication timings around sites.

 

I found that I have to explicitly name the domain controller to which I am making the calls. This way I control the domain controller my powershell calls are being made against and know that when making subsequent queries the domain controller has the 'most up-to-date version' of the object (before full replication has taken place).

 

In your example, I would try getting the DC that the computer being renamed is communicating with and pass that through to the -server parameter in the Get-ADComputer command and see if that helps reduce the amount of strange behaviour you experience.

 

Hope this makes sense and helps set you in the right direction for having a more stable script.