$inventory = Import-Csv "E:\MonitoringScripts\HealthCheck\PatStat_Pshell\inventory.csv"
foreach ($line in $inventory) {
$server_name = $($line.servername)
$port_number = $($line.port)
$resolved_true = [System.Net.Dns]::GetHostAddresses("$server_name")
#Write-Host $resolved_true
if ($resolved_true) {
#Write-Host $server_name
Write-Host 'the host is resolving'
} else {
Write-Host 'Not found in DNS'
}
}
In the above code, how do I avoid the below content to appear in the command promt when there is a host in the inventory file which is not resolving the dns?
Exception calling "GetHostAddresses" with "1" argument(s): "No such host is
known"
At E:\MonitoringScripts\HealthCheck\PatStat_Pshell\patrol.ps1:9 char:2
+ $resolved_true = [System.Net.Dns]::GetHostAddresses("$server_name")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SocketException
Not found in DNS
the host is resolving
I just want to see:
Not found in DNS
or
the host is resolving
$server_name. How to you run this code?if (Test-Connection -ComputerName $server_name) {