I have a newer script that was designed with performance in mind and capable of pinging thousands of host in just a few minutes. The script on this page works fine but if you need a high performance pinger, I highly recommend the newer script.
Check it out here: http://poshtips.com/2011/03/28/bgping-a-high-performance-bulk-ping-utility/
This script has come in handy for me more than once. Key features include:
- Built-in usage help (use -? or -H for help)
- Verbose 0r Quiescent modes
- Ability to show only SUCCESS or FAILed (or both) results
- Returns IP Address and Fully Qualified Domain Name
- Ping Host Name(s) and/or IP Address(es)
- Ability to read host list from the command line and/or a text file
- Produces CSV output for easy data manipulation (GREAT for handling huge lists)
- One quick ping attempt per host for fast list processing
To use, simply save the following text into a file (call it “ping.ps1″) and execute from PowerShell using syntax ./ping hostname or use ./ping -? for help
Enjoy the script and share with anyone you like – please just leave it as is with the PoshTips author name intact and add your own initials to the maintenance log if you tweak it. Hey, if you break it, I want to make sure you get credit for it, not me! ![]()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | $psScriptName = "ping.ps1" $psScriptAuth = "Kahuna at PoshTips.com" $psScriptDate = "11/12/2009" $script:qMode = -1 ############################################################################## ## Ping.ps1 - powershell script to ping systems ## ## Original Script by Kahuna at PoshTips.com - 11/12/2009 ## ## Maintenance: ## Date By Comments ## ---------- --- ---------------------------------------------------------- ## 11/12/2009 KPS New Script ## ############################################################################## $scriptUsage = @" Purpose: This script will retrieve a quick (one ping) network response Usage: ./ping server hostlist.txt [-SHOW=ALL|FAIL|SUCCESS][-Q][-?|-H] Where: -SHOW=ALL displays all ping results (default) -SHOW=FAIL displays only failed ping results -SHOW=SUCCESS displays only successful ping results -V verbose mode (produce output of timestamps and script info) -? or -H displays this help Comments: From the PowerShell command-line, invoke the script, passing any combination of hostnames and filenames (text file(s) containing a hostname listing Any combinations and number of arguments can be used if an argument is a filename: each line in the file will processed as a hostname any lines starting with `"#`" will be treated as comments and ignored if an argument is not a filename: the argument will processed as a hostname Example: ./ping server1 hostlist.txt server2 server3 -SHOW=FAIL "@ ############################################################################## Set-PSDebug -Trace 0 $script:recCount = 0 $script:paramSHOW = "ALL" function ShowScriptInfo() { "########################################################" [string]::Format("## PowerShell Script : {0}", $psScriptName) [string]::Format("## Written By : {0}", $psScriptAuth) [string]::Format("## Last Updated on : {0}", $psScriptDate) "########################################################" } function TimeStamp([string]$message) { "########################################################" "## $(Get-Date) $($message)" "########################################################" } function ShowUsage(){ ShowScriptInfo $scriptUsage } function WMIDateStringToDate($Bootup) { [System.Management.ManagementDateTimeconverter]::ToDateTime($Bootup) } function DoPing ([string]$hostname){ $erroractionpreference = "SilentlyContinue" $hostname = $hostname.trimend(".") $fqdn = [System.Net.Dns]::GetHostEntry($hostname).HostName $ping = new-object System.Net.NetworkInformation.Ping $reply = $ping.send($hostname) if ($reply.status -eq "Success"){ [string]::Format("OK,{0},{2}",$reply.Address.ToString(),$reply.RoundtripTime,$fqdn) } else{ $z = [system.net.dns]::gethostaddresses($hostname)[0].ipaddresstostring [string]::Format("FAIL,{0},{2}",$z,"***",$fqdn) } } ###################################################### ## MAIN ###################################################### ## ## Process command-line arguments ## if ($args.count) { # Check for command-line flags foreach ($item in $args) { #$item if ($item.toupper() -like "-SHOW=*"){ $x = $item.toupper() $paramShow = $x.replace("-SHOW=","") if (!(($paramShow -eq "ALL") -OR ($paramShow -eq "SUCCESS") -OR ($paramShow -eq "FAIL"))){ ShowUsage exit } } if (($item.toupper() -eq "-H") -or ($item.toupper() -eq "-?")){ ShowUsage exit } if ($item.toupper() -eq "-V") {$qMode = 0} } if ($args.count){ if (!($qMode)){ ShowScriptInfo TimeStamp("SCRIPT STARTED") } "LN,HostName,PingStatus,IP,FQDN" # Loop through each argument foreach ($item in $args) { if ((!($item.Contains("SHOW=")) -AND (!($item.toupper() -eq "-Q")))) { # # if argument is a filename, process the contents of the file, ignoring any comment lines # if (test-path($item)) { if (!($qMode)) {[string]::Format("##`n## Processing contents of file: '{0}'`n##",$item)} foreach ($hostname in get-content $item) { $hostname = $hostname.trim() if (($hostname -notlike "#*") -and ($hostname -notlike "")) { $recCount += 1 $d1 = DoPing($hostname) if (($paramSHOW -eq "ALL") -or (($paramSHOW -eq "FAIL") -and ($d1 -like "FAIL*")) -or (($paramSHOW -eq "SUCCESS") -and ($d1 -like "OK*"))) { [string]::Format("{3},{0},{1}{2}", $hostname,$d1,"",$recCount) } } } if (!($qMode)) {[string]::Format("##`n## End of file: '{0}'`n##",$item)} } # # if argument is NOT a filename, treat the argument as a hostname # else { $recCount += 1 $d1 = DoPing($item) if (($paramSHOW -eq "ALL") -or (($paramSHOW -eq "FAIL") -and ($d1 -like "FAIL*")) -or (($paramSHOW -eq "SUCCESS") -and ($d1 -like "OK*"))) { [string]::Format("{3},{0},{1}{2}", $item,$d1,"",$recCount) } } } } } } if (!($qMode)) { TimeStamp("SCRIPT ENDED") } exit |
Looking for a script to do a continuous ping against one host? If so, check this out: http://poshtips.com/2009/12/19/pingmon-a-powershell-ping-monitor-script/
hi,
i’m not a scripting guys kind of like..
but can i use your script to monitor 1 set of ip address for my routers? it is more than 100 ip.
pls advise
This is probably not the best script (as is) for continuous monitoring since it only hits each host/address one time. The intention was to be more of a quick, one-time pass over a list of host to determine the current state.
However, you could modify the script to run in a continuous loop and raise an alert via email if a host/address does not respond. Check out my “how to send email ” post for details:
http://poshtips.com/2009/11/09/sending-email-from-a-powershell-script/
Hope this helps!