I like to target a specific domain controllers when running scripts that interact with Active Directory. For example, if I wanted to get information about a AD user account I can use
Get-ADUser in PowerShell. When I run,
Get-ADUser -Name john for example, I have no idea which domain controller the script is getting it's information from. You can run
Get-ADUser -Name john -Server DC1 to specify a domain controller. The only downside is if I retire DC1 then my script will fail so I need to come up with something more dynamic. I like to target the PDC emulator in the domain if the script is running in the same site as the PDC emulator. Otherwise, if the PDC emulator isn't in the same site then I want to use the domain controller that is closest to me.
Again the wrong way to pick a server:
Get-ADUser -Name john -Server DC1
To use the PDC emulator, I created a variable called $pdc. This is my preferred method for targeting a domain controller in scripts.
$pdc = (Get-ADDomain).PDCEmulator
Get-ADUser -Name john -Server $pdc
To use the domain controller in a site, I create a variable called $remoteCampusDC. I do have to fill in the site name which in this example is remoteCampus. This is my less preferred method for targeting domain controllers.
$remoteCampusDC =(Get-ADDomainController -Discover -SiteName "remoteCampus").name
Get-ADUser -Name john -Server $remoteCampusDC
Overall, this shouldn't add much time to a script and if you need to make multiple calls to the domain controllers then this method could save you time overall.