$hostname = $env:COMPUTERNAME $ad_dc = Get-ADDomainController $dc_digit = $hostname.Substring($hostname.Length - 1) # If a DC has a FSMO Role if($ad_dc.OperationMasterRoles -ne "") { #Get list of FQDN Domain Controllers in domain $domainctrl = Get-ADDomain | Select-Object ReplicaDirectoryServers # Find the DC that ends in 1 or 2 for ($i = 0; $i -lt $domainctrl.ReplicaDirectoryServers.Count; $i++) { $dc_hostname = $domainctrl.ReplicaDirectoryServers[$i].split("{.}") $dc_h_digit = $dc_hostname[0].Substring($dc_hostname[0].Length - 1) if($dc_h_digit -eq 1) { $dc_one = $dc_hostname[0] $one = $i } if($dc_h_digit -eq 2) { $dc_two = $dc_hostname[0] $two = $i } } # If a none 1 or 2 DC has FSMO role, moves FSMO role to 1 if($dc_digit -ne 1 -and $dc_digit -ne 2) { Move-ADDirectoryServerOperationMasterRole -Identity $dc_one -OperationMasterRole SchemaMaster,RIDMaster,InfrastructureMaster,DomainNamingMaster,PDCEmulator -Confirm:$False } if($dc_digit -eq 1) { Move-ADDirectoryServerOperationMasterRole -Identity $dc_two -OperationMasterRole SchemaMaster,RIDMaster,InfrastructureMaster,DomainNamingMaster,PDCEmulator -Confirm:$False } if($dc_digit -eq 2) { Move-ADDirectoryServerOperationMasterRole -Identity $dc_one -OperationMasterRole SchemaMaster,RIDMaster,InfrastructureMaster,DomainNamingMaster,PDCEmulator -Confirm:$False } }