1

我正在使用 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
4

1 回答 1

2

只需捕获异常:

try {
    $addresses = [System.Net.Dns]::GetHostAddresses($server);
}
catch {
    $addresses = [IPAddress]'0.0.0.0';
}
于 2014-12-19T20:25:48.640 回答