我正在使用powershell脚本来查找未在我们的服务器上运行或指向其他服务器的网站。我从文件中获取所有网站名称,并使用它来查找那些未在我们的服务器上运行的网站。我试图使用下面的脚本但是收到错误。
与往常一样,我们非常感谢您的帮助或建议。
$servers = get-content "path_to_the_file"
foreach ($server in $servers) {
$addresses = [System.Net.Dns]::GetHostAddresses($server)
foreach($a in $addresses) {
"{0},{1}" -f $server, $a.IPAddressToString
}
}
以下是我得到的错误:
Exception calling "GetHostAddresses" with "1" argument(s): "No such host is known" At
C:\test1.ps1:3 char:50 + $addresses = [System.Net.Dns]::GetHostAddresses <<<< ($server) +
CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId :
DotNetMethodException
答案 0 :(得分:2)
只是捕获异常:
try {
$addresses = [System.Net.Dns]::GetHostAddresses($server);
}
catch {
$addresses = [IPAddress]'0.0.0.0';
}