After needing to ping large lists of servers I created a quick function to do exactly that. It will accept pipeline input or you can provide server names via an input parameter. It also has input parameters for buffer size and count as well as creation of a logfile for output. This is a function I probably use on more or less a daily basis. It’s incredibly useful due to it being pipeline enabled.
As per the examples in the comments, you can pipe the content of the textfile in and the function will go away and ping all machines in the list. I also use it to ping all machines on a VMware host quite frequently if the host’s management agents have died. If the -Count parameter is set to 1 then the ping results will come back incredibly quickly. Essentially you can ping 50+ servers in a couple of seconds. Feel free to suggest improvements.
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 |
function Ping-RemoteHosts { <# .SYNOPSIS Performs a ping request to one or more server names or IP addresses and returns the results with optional output to file. This command accepts parameterised server names or will accept pipelined input from a file or server list from another command. .DESCRIPTION Ping-RemoteHosts sends an ICMP request to the specified hosts or IP addresses. .PARAMETER ComputerName One or more computer names or IP addresses. .PARAMETER BuffSize Sets the number of packets to send in the ping request. .PARAMETER Count Sets the number of ping requests to send to the remote host. .PARAMETER LogOutput Specify this switch to create a text log file of results of the ping requests. .PARAMETER OutputLog When used with -LogOutput the command will generate an output log of ping results. Defaults to C:\Temp as output location. .PARAMETER Full Sets the output to display the full information from the ping request rather than the summarised default. .EXAMPLE Get-Content servernames.txt | Ping-RemoteHosts .EXAMPLE Ping-RemoteHosts -ComputerName SERVER1,SERVER2 .EXAMPLE Ping-RemoteHosts -ComputerName SERVER1,SERVER2 -LogOutput -Count 1 -BuffSize 32 | Select-Object ComputerName,ComputerIp4,PingStatus,TimeRes #> [CmdletBinding()] param( [Parameter(Mandatory = $True, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True, HelpMessage="Computer name or IP")] [string[]]$ComputerName, [int]$BuffSize = 16, [int]$Count = 3, [string]$OutputLog = "C:\Temp\Ping-RemoteHosts_Output_$(Get-Date -Format "MM-dd-yyyy_HH-mm-ss").log", [switch]$LogOutput, [switch]$Full ) BEGIN { if ($LogOutput){ "ComputerName".PadRight(25) + ` "ComputerIp4".PadRight(19) + ` "PingStatus".PadRight(18) + ` "Time(ms)" | Out-File $OutputLog "".PadRight(70,"-") | Out-File $OutputLog -Append } } PROCESS { foreach($Computer in $ComputerName) { Try { $PingTest = Test-Connection -ComputerName $Computer -BufferSize $BuffSize -Count $Count -ErrorAction Stop foreach($Ping in $PingTest) { $PingOut = @{ 'ComputerName' = $Computer; 'ComputerIp4' = $Ping.IPV4Address.IPAddressToString; 'PingStatus' = "OK" 'BytesSent' = $Ping.BufferSize; 'TimeRes' = $Ping.ResponseTime; } } $PingObj = New-Object -TypeName PSObject -Property $PingOut $PingObj.PSObject.TypeNames.Insert(0,'DzAdmToolsPS.Ping-RemoteHosts'); if($Full) { Write-Output $PingObj } else { Write-Output $PingObj | Select-Object ComputerName,PingStatus } if($LogOutput) { $PingObj.ComputerName.PadRight(25) + ` $PingObj.ComputerIp4.PadRight(19) + ` $PingObj.PingStatus.PadRight(18) + ` $PingObj.TimeRes | Out-File $OutputLog -Append } } Catch { $DnsLookup = Resolve-DNSName -Name $Computer -ErrorAction SilentlyContinue if($DnsLookup.IPAddress -eq $null) { $IpAddress = "N/A" } else { $IpAddress = $DnsLookup.IPAddress } $PingOut = @{ 'ComputerName' = $Computer; 'ComputerIp4' = $IpAddress; 'PingStatus' = "DOWN" 'BytesSent' = $BuffSize; 'TimeRes' = "N/A"; } $PingObj = New-Object -TypeName PSObject -Property $PingOut $PingObj.PSObject.TypeNames.Insert(0,'DzAdmToolsPS.Ping-RemoteHosts'); Write-Output $PingObj if($LogOutput) { $PingObj.ComputerName.PadRight(25) + ` $PingObj.ComputerIp4.PadRight(19) + ` $PingObj.PingStatus.PadRight(18) + ` $PingObj.TimeRes | Out-File $OutputLog -Append } } } } END { } } |