Skip to content

Instantly share code, notes, and snippets.

@hpmmatuska
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hpmmatuska/1326a7b43fc2484f274b to your computer and use it in GitHub Desktop.
Save hpmmatuska/1326a7b43fc2484f274b to your computer and use it in GitHub Desktop.
Test Connection with multiple features
Function Test-Ping {
<#
.Synopsis
Do a simple conncetion test (ping) to specified hosts.
.Description
The command will run "test-connection" again hosts defined in variable. If Single host is specified,
there will run continuos ping, otherwise one packet will be send to all hosts in paralel. The pipe input is supported.
.Parameter ComputerName
The destination name or IP addresses
alias: name, cn
.Parameter DelayInMilliseconds
The delay between connection tests for single host. Default value is 1000.
alias: d
.Parameter BufferSize
The size of the sent packet. Defualt size is 32b.
alias: b
.Parameter TimeToLive
Specified maximum time to wait for answer. Default value is 80 ms.
alias: ttl
.Paramater ThrottleLimit
The number of concurrent connection test. The default is 32 connections.
alias: tl
.Parameter Range
Testing bundle of IPv4, where start address is the entered. The range param will increment start address by one.
The input is expected only IPv4 address.
.Switch ShowOnlyFailed
When connection test runs again single IP, this option will display only missed answers.
.Example
PS C:\> test-ping server
will perform connection test in a loop to entered name or IP
.Example
PS C:\> test-ping server, 10.1.1.1 -ThrottleLimit 8
will perform single connection test to entered names or IPs in budle by 8 servers.
.Example
PS C:\> test-ping server -DelayInMillisecond 500 -BufferSize 16 -TimeToLive 40
will perform endless connection test to entered names or IPs with specified parameters
.Example
PS C:\> test-ping 10.0.0.0 -Range 50
will test 50 IP addresses for response, starting at 10.0.0.0 and ending with 10.0.0.50
.Example
PS C:\> test-ping server -DelayInMillisececonds 3000 -ShowOnlyFailed
suitable for basic availability of the server with negative results only (of course with timestamp)
.Notes
Last Updated: April 17, 2015
Version : 1.0
#>
[cmdletbinding()]
Param(
[Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[ValidateNotNullorEmpty()]
[Alias("cn","name")][String[]]$ComputerName,
[Alias("d")][int]$DelayInMilliseconds=1000,
[Alias("b")][int]$BufferSize=32,
[Alias("ttl")][int]$TimeToLive=80,
[Alias("tl")][int]$ThrottleLimit=32,
[int]$Range=0,
[Switch]$ShowOnlyFailed
)
If ($ComputerName.Count -eq 1){
if ($range -eq 0) {
if (!$ShowOnlyFailed) {
write-host ""
write-warning "Endless ping is running, to break the cycle press CTRL+C"
write-host ""
}
else {
write-host ""
write-warning "Endless ping is running with parameter to show only missed reply, to break the cycle press CTRL+C"
write-host ""
}
do {
try{
$ping = Test-Connection -Count 1 -BufferSize $BufferSize -TimeToLive $TimeToLive -ComputerName $ComputerName -ErrorAction stop
if(!$ShowOnlyFailed) {
write-host `
(get-date).ToString() + `
"`tReply from: " + $ping.Address + `
" `t " + $ping.IPV4Address + `
" `t " + $ping.IPV6Address + `
" `tBuffer Size: " + $ping.ReplySize + `
" `tResponseTime (ms): " + $ping.ResponseTime
}
}
catch {Write-Warning ((Get-Date).ToString()+"`t" + $_.Exception.Message)}
Start-Sleep -m $DelayInMilliseconds
} while ($true)
} # if range = 0
else {
$pattern = "^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$" ##REGEX test for IP
if ($ComputerName -match $pattern){ #REGEX test for IP
$ipbase = ($computername.Split('.'))[0]+'.'+ ($computername.Split('.'))[1] +'.'+ ($computername.Split('.'))[2] + '.'
( ([int]($computername.Split('.')[3])) .. ([int](($computername.Split('.'))[3])+$range) ) | %{
if ($_ -le 255) {
$ping = Test-Connection -ComputerName ($ipbase + $_.ToString()) -Count 1 -Quiet -ErrorAction SilentlyContinue
if ($ping -eq $true){write-host ($ipbase+ $_ + "`tresponding")}
else {write-warning ($ipbase + $_ + "`tnot responding")}
}
else {Write-Warning ($ipbase + $_ + "`tis not valid IP.")}
}
} #REGEX test for IP
else {write-warning "$ip is not an valid IPv4 address. We are able to proceed only IPv4 with parameter 'Range'"}
} # if range ne 0
} # if one host
else {
if ($range -gt 0) {Write-Warning "for the more hosts is Range parameter ignored"}
$job = Test-Connection -Count 1 -BufferSize $BufferSize -TimeToLive $TimeToLive -ThrottleLimit $ThrottleLimit -ComputerName $ComputerName -AsJob
do {Start-Sleep -m 80} while ($job.JobStateInfo.State -eq "Running")
Receive-Job $job
} # more hosts
} #end function ping
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment