0
$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
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
kaushik km
  • 39
  • 8
  • First you should remove the quotes around the variable `$server_name`. How to you run this code? – Olaf Feb 25 '19 at 18:22
  • PS E:\MonitoringScripts\HealthCheck\PatStat_Pshell> .\patrol.ps1 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") | foreach { ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : SocketException Not found in DNS the host is resolving – kaushik km Feb 25 '19 at 18:28
  • Its running fine event with out removing the double quots. but as suggested i will go ahead and remove it. – kaushik km Feb 25 '19 at 18:29
  • take a look at the 3rd answer here ... Powershell: How can I stop errors from being displayed in a script? - Stack Overflow — https://stackoverflow.com/questions/8388650/powershell-how-can-i-stop-errors-from-being-displayed-in-a-script – Lee_Dailey Feb 25 '19 at 18:31
  • You might use `if (Test-Connection -ComputerName $server_name) {` – lit Feb 25 '19 at 18:32
  • @Lee_Dailey I could not get it.. quite new to scripting. my main concern is to not to get the error when the host does not resolve. could you please modify the above lines ? – kaushik km Feb 25 '19 at 18:43
  • @lit I am not just checking for icmp connection i want to check if the host resolves to an IP address. so i cannot use `Test-Connection` – kaushik km Feb 25 '19 at 18:45
  • @kaushikkm - simply add the function to the top of your script and then replace calls to that static method with the function call. [*grin*] – Lee_Dailey Feb 25 '19 at 18:50
  • @kaushikkm - Are you saying that you might find it in DNS even if the host is not up and available? – lit Feb 25 '19 at 18:50

4 Answers4

1

Catch the exception:

try {
    [Net.Dns]::GetHostAddresses($server_name)
    Write-Host 'the host is resolving'
} catch {
    Write-Host 'Not found in DNS'
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • 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: (:) [], ParentContainsErrorRecordE xception + FullyQualifiedErrorId : SocketException – kaushik km Feb 25 '19 at 18:56
  • yes @Ansgar Wiechers, i am trying to post the code which i tried.. unable to post it with proper indentation – kaushik km Feb 25 '19 at 19:11
  • Just click on the "edit" link right below your question and add your modified code. BTW, what output does `$PSVersionTable` give you? Include that output as well. – Ansgar Wiechers Feb 25 '19 at 19:14
  • here it was executing the command and giving the output of `[Net.Dns]::GetHostAddresses($server_name)` which is not required – kaushik km Feb 25 '19 at 19:29
  • PSVersion 3.0 WSManStackVersion 3.0 SerializationVersion 1.1.0.1 CLRVersion 4.0.30319.36470 BuildVersion 6.2.9200.22198 – kaushik km Feb 25 '19 at 19:30
  • @kaushikkm Then you did not run the command in a `try..catch` statement. Plain and simple. – Ansgar Wiechers Feb 25 '19 at 21:57
0

Test-Connection to see if the host exists first. Of course, the host existing is not exactly the same thing as finding it in DNS.

$inventory = import-csv “E:\MonitoringScripts\HealthCheck\PatStat_Pshell\inventory.csv”
ForEach ($line in $inventory)
{
    $server_name = $($line.servername)
    $port_number = $($line.port)
    $resolved_true = $null
    if (Test-Connection -ComputerName $server_name -ErrorAction SilentlyContinue) {
        $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'
    }
}
lit
  • 14,456
  • 10
  • 65
  • 119
  • Test-connection started to throw error, as it was not able to find the host. i just do not want the error to be thrown, let it go to the else condition – kaushik km Feb 25 '19 at 18:50
  • PS E:\MonitoringScripts\HealthCheck\PatStat_Pshell> .\patrol2.ps1 Test-Connection : Testing connection to computer 'vlhebsadv06abc' failed: The requested name is valid, but no data of the requested type was found At E:\MonitoringScripts\HealthCheck\PatStat_Pshell\patrol2.ps1:7 char:9 + if (Test-Connection $server_name) { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ResourceUnavailable: (vlhebsadv06abc:String) [Te st-Connection], PingException + FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Com mands.TestConnectionCommnd – kaushik km Feb 25 '19 at 18:52
  • how to use -ErrorAction ? – kaushik km Feb 25 '19 at 18:52
  • wow this is what i was looking for. Thank you. not sure why.. but it is taking very long time to execute. any idea ? or how to improve ? i am just running this for two hosts.. and it took 4-5 seconds i would want to run this on more than 2K hosts – kaushik km Feb 25 '19 at 18:58
  • @kaushikkm - Actually, the try/catch exception handling approach would be more direct. I am surprised it did not work for you. – lit Feb 25 '19 at 19:00
0

Read About Try Catch Finally:

Use Try, Catch, and Finally blocks to respond to or handle terminating errors in scripts.

You can apply it as follows:

try {
    $resolved_true = [System.Net.Dns]::GetHostAddresses($server_name)
} catch {
    $resolved_true = $null
}
JosefZ
  • 28,460
  • 5
  • 44
  • 83
0
$inventory = import-csv “E:\MonitoringScripts\HealthCheck\PatStat_Pshell\inventory.csv”
ForEach ($line in $inventory)
{
    $server_name = $($line.servername)
    $port_number = $($line.port)
    $resolved_true = $null
    try {
    $resolved_true = [System.Net.Dns]::GetHostAddresses($server_name) 
    } catch {
    $resolved_true = $null
    }
    #Write-host $resolved_true
    if($resolved_true) {
        #write-host $server_name
        Write-Host 'the host is resolving'
    } else {
        Write-Host 'Not found in DNS'
    }
}

This worked for me, Thank you so much all for the help. I will have to mark @JosefZ Answer, But really thank you all.

kaushik km
  • 39
  • 8