Script to Ping/Resovle names given IP

Scotch Tech
Scotch Tech used Ask the Experts™
on
I have a list of IP's that I need to get the computer names for and status. How can I script this?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Systems Engineer
Commented:
Here is the script

#Get List of IPs to Check
$InputFile = 'e:\Temp\list.txt'
$IPaddresses = get-content $InputFile
$reader = New-Object IO.StreamReader $InputFile
    while($reader.ReadLine() -ne $null){ $TotalIPs++ }
write-host    ""    
write-Host "Performing Checks on each IP address..."    
        foreach($IP in $IPaddresses) {
            ## Progress bar
            $i++
            $percentdone = (($i / $TotalIPs) * 100)
            $percentdonerounded = "{0:N0}" -f $percentdone
            Write-Progress -Activity "Performing nslookups" -CurrentOperation "Working on IP: $IP (IP $i of $TotalIPs)" -Status "$percentdonerounded% complete" -PercentComplete $percentdone
            ## End progress bar
            try {
                [system.net.dns]::resolve($IP) | Select HostName,AddressList
                }
                catch {
                    Write-host "$IP was not found. $_" -ForegroundColor Green
                }
            }
write-host    ""            
write-Host "Pinging each address..."
        foreach($IP in $IPaddresses) {
            ## Progress bar
            $j++
            $percentdone2 = (($j / $TotalIPs) * 100)
            $percentdonerounded2 = "{0:N0}" -f $percentdone2
            Write-Progress -Activity "Performing pings" -CurrentOperation "Pinging IP: $IP (IP $j of $TotalIPs)" -Status "$percentdonerounded2% complete" -PercentComplete $percentdone2
            ## End progress bar
                if (test-Connection -ComputerName $IP -Count 2 -Quiet ) {  
                    write-Host "$IP responded" -ForegroundColor Green 
                    } else 
                    { Write-Warning "$IP does not respond to pings"              
                    }  
        }
write-host    ""        
write-host "Done!"

Open in new window

Chris DentPowerShell Developer
Top Expert 2010

Commented:
Shorter, but without progress.
function Get-State {
    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline = $true)]
        [String]$IPAddress
    )

    process {
        try {
            $HostName = [System.Net.Dns]::GetHostEntry($IPAddress).HostName
        } catch {
            $HostName = ""
        }

        [PSCustomObject]@{
            IPAddress      = $IPAddress
            HostName       = $HostName
            RespondsToPing = Test-Connection $IPAddress -Quiet -Count 2
        }
    }
}

Get-Content TheListOfIPs.txt | Get-State

Open in new window

Author

Commented:
Damn, those are some intimidating looking scripts! Thanks, I'll test them both out.

Author

Commented:
thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial