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

Posted on 2011-10-12
Last Modified: 2012-06-27
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.

$computers= gc C:\ListOfDevices.txt
foreach ($computername in $computers) {
  $HostName = [System.Net.Dns]::GetHostByAddress($computername).HostName
  $IP = [System.Net.Dns]::GetHostAddresses($computername)
  Trap {
   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

    LVL 25

    Expert Comment

    LVL 6

    Author Comment

    by:Senior IT System Engineer
    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 ?
    LVL 5

    Accepted Solution

    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 = ([]::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)
    $comp |
    select @{Name='ComputerName';Expression={$comp}}, `
    @{Name='ResolvesToIP';Expression={[]::GetHostAddresses($comp)}}, `
    @{Name='IPResolvesTo';Expression={([]::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

    LVL 6

    Author Closing Comment

    by:Senior IT System Engineer
    Yes it works man !

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
    You might have come across a situation when you have Exchange 2013 server in two different sites (Production and DR). After adding the Database copy in ECP console it displays Database copy status unknown for the DR exchange server. Issue is strange…
    This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…
    This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now