2

我有一个每天运行的 powershell 脚本。今天它失败了,因为我使用的域控制器不可用。在继续执行脚本的其余部分之前,我想确保我可以连接到可用的 DC。

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

今天上面的代码抛出了一个错误“无法联系服务器”。我可以让 $ldapserver 填充多个 DC,但我不确定如何获得可用的 DC,因此其余代码不会失败。因此,如果列出的第一个 dc 服务器出现故障,它将转到下一个 dc 服务器。如果下一个 dc 服务器良好,则确定可用的 dc 可以停止并使用可用的 DC 更新 $activeLDAP。可能吗?

4

1 回答 1

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

您可以使用它在运行时查找服务器(并忽略缓存的结果)。-Service ADWS只需确保它正在运行 AD cmdlet 使用的 Active Directory Web 服务。

于 2015-09-21T15:37:27.020 回答