7

All the PowerShell cmdlets in the ActiveDirectory module support being run against a specific domain controller using the -server parameter; but is there any way to set a default DC to use for all AD-related operations, or do I need to specify it on every single command if I actually care about which DC I'm using (as is frequent when replication latency is involved)?

6
  • Are you wanting to run commands on a distant DC, or always your closest? Jul 31, 2013 at 9:52
  • I just want to make sure all commands always talk to the same DC, in order to avoid replication issues. Selecting the DC is not a problem, it will be provided in a variable.
    – Massimo
    Jul 31, 2013 at 10:03
  • Also, in the same script(s) I'm running Exchange-related commands, and I need to make sure AD commands and Exchange commands are executed against the same DC. This is easy to do for Exchange (Set-ADServerSettings), but I can't find a way to do it for AD...
    – Massimo
    Jul 31, 2013 at 10:04
  • Do you have your AD sites set up correctly?
    – longneck
    Jul 31, 2013 at 11:40
  • Yes, but even with that, a different DC in the same site could be used by different commands. I'm not concerned about properly locating the nearest domain controller; I want to globally set which one all AD cmdlets should use.
    – Massimo
    Jul 31, 2013 at 15:05

2 Answers 2

6

If you are on PowerShell version 3, then you can use the new automatic variable $PSDefaultParameterValues to set a default for the Server parameter on the AD Module cmdlets. You can run

Get-Help about_Parameters_Default_Values

for more details on this variable.

In your specific case, you could set the variable like so:

$PSDefaultParameterValues = @{"*-AD*:Server"='YOUR-CHOSEN-DC'}

Another option which would work with version 2 or 3, is to use the AD Module's provider to create a new PSDrive.

By default when you import the AD Module, it creates an "AD:" PSDrive which connects to the local domain. You can create new PSDrives using this same provider, specifying the specific domain controller you want to connect to. Then, when you run AD cmdlets from within the context of that PSDrive, they will use that connection. You can create a new PSDrive like so:

New-PSDrive -Name <name of the drive> -PSProvider ActiveDirectory -Root "<DN of the partition/NC>" –Server <server or domain name (NetBIOS/FQDN)[:port number]> -Credential <domain name>\<username>

Then just cd <name of drive>: and when you run your cmdlets, they will use the domain controller you specified in the New-PSDrive cmdlet.

3
  • I thought about creating a custom PSDrive, but I couldn't find any evidence whatsoever that it actually influences AD-related cmdlets. Proof: if I delete the default one with Remove-PSDrive AD, those cmdlets are able to run perfectly fine without having any "drive" defined.
    – Massimo
    Aug 5, 2013 at 20:23
  • True -- AD provider drive is not required for the AD cmdlets to run, but IF they are called from within the context of one, that connection is used for the cmdlet. So if you create a new drive and use Set-Location (cd) to move to it, then the AD cmdlets will use the properties of that drive.
    – jbsmith
    Aug 5, 2013 at 20:31
  • 2
    One drawback to @{"-AD:Server"='YOUR-CHOSEN-DC'} is that there can be other non-AD cmdlets that start the noun with "AD" such as the LAPS PowerShell cmdlets like Get-AdmPwdPassword. Right now those are the only ones I've found and fortunately there are no AD cmdlets that begin with "ADM" so I've worked around this by using "-AD[a-ln-z]:Server",$((Get-ADDomain).PDCEmulator) as my value which skips any cmdlet nouns starting with "ADM". I tried using just -AD[^m] in the construction but the negation didn't work, not sure why.
    – CitizenRon
    Aug 22, 2017 at 19:35
1

I know the post is old but i would like to share what i did with the solution posted by jbsmith to make it work only for ActiveDirectory module commands:

$Module = "ActiveDirectory"
$Server = Read-Host "Enter server to use for $Module"
$i=0
foreach ($CommandName in (Get-Command -ParameterName Server -Module ActiveDirectory).Name) {
    $PSDefaultParameterValues += @{"$($CommandName):Server"=$Server;$i++}
}
foreach ($CommandName in (Get-Command -ParameterName DomainController -Module ActiveDirectory).Name) {
    $PSDefaultParameterValues += @{"$($CommandName):DomainController"=$Server;$i++}
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .