• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 546
  • Last Modified:

Need help in correcting Powershell script to check ping response UP/Down

Hi People,

Can Anyone help me in correcting or modifying my current script which can ping and resolve Server name into IP address and vice versa ?

The problem at the moment is that it is running slowly and when the host is OFFLINE, the IP address is still assigned in the variable and then passed on to the next host which causing me confusion ?

Any help would be greatly appreciated.

Thanks.
$computers= gc C:\ListOfDevices.txt
foreach ($computername in $computers) {
  $HostName = [System.Net.Dns]::GetHostByAddress($computername).HostName
  $IP = [System.Net.Dns]::GetHostAddresses($computername)
  Trap {
        Continue
  }
   if (Test-Connection $computername -erroraction SilentlyContinue -Count 1 ) {
     write-host "$computername - $IP - UP" -ForegroundColor GREEN
   }
   else {
     write-host "$computername - $IP - DOWN" -ForegroundColor RED
   }
 }

Open in new window

0
Senior IT System Engineer
Asked:
Senior IT System Engineer
  • 2
1 Solution
 
Tony JLead Technical ArchitectCommented:
0
 
Senior IT System EngineerIT ProfessionalAuthor Commented:
Thanks for the reply Tony, however the script still got a minor logic error

1. if one of the host NAME in the list is DOWN, the IP address of the DOWN host is still reused in the variable for the next DOWN host, if this variable can be reset to NO-IP or something else that'd be great.

2. if the host IP address in the list (UP or DOWN) they are not converted into DNSName eventhough the DNS entry in NSLOOKUP returns the correct value ?
0
 
daveTechSearchCommented:
How about something like this?

Note: I've left in the 'stuff' that I use for myself, such as the progress.... easily removed.
$counter = 1
$comps = get-content "C:\ListOfDevices.txt"
$dnsResults = "C:\get-dnsres.csv"

function get-dnsres{

foreach ($comp in $comps)
{
$TempIP = ([system.net.dns]::GetHostAddresses($comp)) | select IPAddressToString

     $status = "Processing system {0} of {1}: {2}" -f $counter,$comps.Count,$comp
     Write-Progress 'Resolving DNS' $status -PercentComplete ($counter/$comps.count * 100)
     $counter++    
$comp |
select @{Name='ComputerName';Expression={$comp}}, `
@{Name='ResolvesToIP';Expression={[system.net.dns]::GetHostAddresses($comp)}}, `
@{Name='IPResolvesTo';Expression={([system.net.dns]::GetHostEntry($TempIP.IPAddressToString)).HostName}}, `
@{Name='PingStatus'; `
Expression={ `
       if ((get-wmiobject -query "SELECT * FROM Win32_PingStatus WHERE Address='$comp'").statuscode -eq 0) {'Host Online'} `
    elseif ((get-wmiobject -query "SELECT * FROM Win32_PingStatus WHERE Address='$comp'").statuscode -eq 11003) {'Destination Host Unreachable'} `
    elseif  ((get-wmiobject -query "SELECT * FROM Win32_PingStatus WHERE Address='$comp'").statuscode -eq 11010)  {'Request Timed Out'} `
    elseif ((get-wmiobject -query "SELECT * FROM Win32_PingStatus WHERE Address='$comp'").statuscode -eq $Null) {'NoDNS'}


           }
}
}
}

get-dnsres | export-csv $dnsResults -notypeinformation
invoke-item $dnsResults

Open in new window

0
 
Senior IT System EngineerIT ProfessionalAuthor Commented:
Yes it works man !
0

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now