Ping Multiple Hosts with PowerShell and Return Useful Information

A lot of our time in IT is spent making sure systems are up and running. Most times our first test when troubleshooting an issue is to perform a PING command. For that reason I have created the script below which in essence just performs the Test-Connection cmdlet. This script only provides me the useful information that I want from the cmdlet, both onscreen and in a csv file.

If you want to get really fancy you could perform a get-qadcomputer command and pipe the output to a CSV file. Then use that CSV file as the input file for the script below. If you did this you would be able to know which machines in your domain can respond to a PING response.


###########################################################################
#
# NAME: Ping-Host.ps1
#
# AUTHOR: Joshua Schofield
#
# COMMENT: If using a CSV file, you must have a column named DNSName in order for script to complete.
#
# EXAMPLE: c:scriptsping-host.ps1 -sourcefile c:scriptscsvservers.csv -oufile c:scriptslogslogfile.csv
#
# VERSION HISTORY: 1
#
# VERSION DATE:    8/21/2012
#
# VERSION COMMENTS: Tested and Validated
#
#
###########################################################################

param (

	[Parameter(Mandatory = $true)]
	$SourceFile,

	[Parameter(Mandatory = $true)]
	$OutFile

)

Function Ping-Hosts {

param ($server)

$test = Test-Connection $server -Count 1 -Quiet -ErrorAction SilentlyContinue
$ip = Test-Connection $server -Count 1 | select ipv4address -ErrorAction SilentlyContinue
$ip = $ip.IPV4Address

if ($test.ToString() -like "true") {

	Write-Host "$server $ip is pingable" -ForegroundColor green
	Write-Output "$server,$ip,yes" | Out-File $OutFile -Append

}
else {
	Write-Host "$server not pingable" -ForegroundColor Red
	Write-Output "$server,$ip,no" | Out-File $OutFile -Append

}

$test = $null
$name = $null
$server = $null
$ip = $null
}

$filetype = $SourceFile.split(".")[1]

Write-Output "ServerName,IP,RespondsToPING" | Out-File $OutFile -force

if ($filetype -eq "txt"){

	gc $sourcefile | % {

		ping-hosts $_

}
}

Elseif ($filetype -eq "csv"){

	Import-Csv $sourcefile | % {

		ping-hosts $_.dnsname

}
}

else{

Write-Host "Filetype: $filetype not recognized. Filetype must be .csv or .txt . Please try again." -ForegroundColor DarkRed -BackgroundColor White

}

Posted in Windows OS and tagged , , , , , , , , , , , , , , , , .

4 Comments

  1. Can you give me more details on how to run this as i have created the CSV files and have run c:scriptsping-host.ps1 -sourcefile c:scriptscsvservers.csv -oufile c:scriptslogslogfile.csv using my paths and file names and i get an error Error being you cannot call a method with null value.

    • Hey there Scott. Can you try running Import-CSV c:scriptscsvservers.csv and let me know if you see the correct data on the screen from your CSV file? Also since your sourcefile is in CSV format is the column heading for your server FQDN labeled: DNSNAME? See below where it calls $_.dnsname

      69 Elseif ($filetype -eq “csv”){
      70
      71 Import-Csv $sourcefile | % {
      72
      73 ping-hosts $_.dnsname

  2. This is a great script. I have a list of IPs. I was wondering if there was a way to manipulate this script to return if the system is pingable, as well as perform a reverse DNS lookup and document the machine name for me

    Thanks

    • If you utilize the following: [System.Net.DNS]::GetHostByAddress($IP) where $IP would be the IP Address of the IP in your list, you could query your DNS environment to find the name of the machine in question.

      You could place this before the true/false statement in the function or within both the true and false loops. Then just add an output for the Hostname which derives from the function listed above.

      This is what you would see if you run System.Net.DNS:

      HostName Aliases AddressList
      ——– ——- ———–
      Computer1 {} {x.x.x.x}

      Add this to the script prior to the True/False foreach loops:
      $Hostname = [System.Net.DNS]::GetHostByAddress($IP)
      $hostname = $hostname.hostname

      Then add $hostname to the output area.

      Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *