问题
I am working on a powershell script to find websites which are either not running on our server or pointing to another server. I am fetching all website names from a file and using it to find only those websites which are not running on our server. I'm trying to use below script but getting an error.
As always, and help or advice would be much appreciated.
$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
}
}
Below is the error I am getting:
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
回答1:
Just trap the exception:
try {
$addresses = [System.Net.Dns]::GetHostAddresses($server);
}
catch {
$addresses = [IPAddress]'0.0.0.0';
}
来源:https://stackoverflow.com/questions/27571709/script-to-find-dead-website-domain