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
9
  • First you should remove the quotes around the variable $server_name. How to you run this code? Commented Feb 25, 2019 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 Commented Feb 25, 2019 at 18:28
  • Its running fine event with out removing the double quots. but as suggested i will go ahead and remove it. Commented Feb 25, 2019 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 — stackoverflow.com/questions/8388650/… Commented Feb 25, 2019 at 18:31
  • You might use if (Test-Connection -ComputerName $server_name) { Commented Feb 25, 2019 at 18:32

4 Answers 4

1

Catch the exception:

try {
    [Net.Dns]::GetHostAddresses($server_name)
    Write-Host 'the host is resolving'
} catch {
    Write-Host 'Not found in DNS'
}
Sign up to request clarification or add additional context in comments.

6 Comments

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
yes @Ansgar Wiechers, i am trying to post the code which i tried.. unable to post it with proper indentation
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.
here it was executing the command and giving the output of [Net.Dns]::GetHostAddresses($server_name) which is not required
PSVersion 3.0 WSManStackVersion 3.0 SerializationVersion 1.1.0.1 CLRVersion 4.0.30319.36470 BuildVersion 6.2.9200.22198
|
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
}

Comments

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'
    }
}

5 Comments

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
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
how to use -ErrorAction ?
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
@kaushikkm - Actually, the try/catch exception handling approach would be more direct. I am surprised it did not work for you.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.