Script to find dead website/domain

微笑、不失礼 提交于 2019-12-11 20:06:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!