2

I have a powershell script that runs everyday. Today it failed because the domain controller i was using became unavailable. Before I can continue with the rest of the script i want to make sure I can connect to a available DC.

$LdapServer = "DC874.model.com"
Get-ADDomainController -server $ldapserver

Today the above code threw an error "unable to contact the server". I can have $ldapserver populated with multiple DC but I am not sure how to get an available DC so rest of the code dont fail. so if the first dc server listed is failed it will go to next dc server. if next dc server is good then determining available dc can stop and update $activeLDAP with the available DC. Is it possible?

Ninja Cowgirl
  • 10,421
  • 8
  • 33
  • 41

1 Answers1

1
$ldapServer = Get-ADDomainController "DC874.model.com" -ErrorAction Ignore
if (!$ldapServer) {
    $ldapServer = Get-ADDomainController -Discover -ForceDiscover -Service ADWS
}

https://technet.microsoft.com/en-us/library/ee617217.aspx

You can use this to find a server at runtime (and ignore cached result). The -Service ADWS just makes sure it's running Active Directory Web Services which is used by the AD cmdlets.

briantist
  • 45,546
  • 6
  • 82
  • 127
  • Cool. it works however, i get an error when the DC is not online. Is there a way to capture the long error in a better way so that if $return = false or something then i can go to next available DC? – Ninja Cowgirl Sep 21 '15 at 16:28
  • @ShawnTorres you get an error even with `-ErrorAction Ignore`? – briantist Sep 21 '15 at 16:29
  • This is error when server is not available. Get-ADDomainController : Unable to contact the server. This may be because this server does not exist, it is currently down, or it does not have the Active Di rectory Web Services running. At D:\powershell\VerifyLDAPServer.ps1:3 char:29 + if (!(Get-ADDomainController <<<< -server $LdapServer)) { + CategoryInfo : ResourceUnavailable: (:) [Get-ADDomainController ], ADServerDownException + FullyQualifiedErrorId : Unable to contact the server. This may be becaus e this server does not exist, it is currently down, – Ninja Cowgirl Sep 21 '15 at 16:46