pi/test.ps1

141 lines
4.1 KiB
PowerShell

<#
.SYNOPSIS
Performs a series of asynchronous pings against the target hosts.
.PARAMETER HostName
A string array of target hosts to ping.
.PARAMETER PingCount
The number of pings to send against each host.
#>
function Invoke-FastPing {
[alias('FastPing', 'fping', 'fp')]
param
(
[Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[Alias('Computer', 'ComputerName', 'Host')]
[String[]] $HostName,
[Int] $PingCount = 4
)
process {
# Objects to hold items as we process pings
$queue = [System.Collections.Queue]::new()
$pingHash = @{}
# Start an asynchronous ping against each computer
foreach ($hn in $HostName) {
if ($pingHash.Keys -notcontains $hn) {
$pingHash.Add($hn, [System.Collections.ArrayList]::new())
}
for ($i = 0; $i -lt $PingCount; $i++) {
$ping = [System.Net.Networkinformation.Ping]::new()
$object = @{
Host = $hn
Ping = $ping
Async = $ping.SendPingAsync($hn)
}
$queue.Enqueue($object)
}
}
# Process the asynchronous pings
while ($queue.Count -gt 0) {
$object = $queue.Dequeue()
if ($object.Async.IsCompleted -eq $true) {
$null = $pingHash[$object.Host].Add(@{
Host = $object.Host
RoundtripTime = $object.Async.Result.RoundtripTime
Status = $object.Async.Result.Status
})
}
else {
$queue.Enqueue($object)
}
}
# Using the ping results in pingHash, calculate the average RoundtripTime
foreach ($key in $pingHash.Keys) {
if (($pingHash.$key.Status | Select-Object -Unique) -eq 'Success') {
$online = $true
}
else {
$online = $false
}
if ($online -eq $true) {
$latency = [System.Collections.ArrayList]::new()
foreach ($value in $pingHash.$key) {
if ($value.RoundtripTime) {
$null = $latency.Add($value.RoundtripTime)
}
}
$average = $latency | Measure-Object -Average
if ($average.Average) {
$roundtripAverage = [Math]::Round($average.Average, 0)
}
else {
$roundtripAverage = $null
}
}
else {
$roundtripAverage = $null
}
[PSCustomObject]@{
ComputerName = $key
RoundtripAverage = $roundtripAverage
Online = $online
}
}
} # End Process
}
# Invoke-FastPing @("ME0001", "ME0025", "ME-Linux-CP") -PingCount 1
# Invoke-FastPing @("ME-Linux-CP") -PingCount 1
# write-host "test1"
# Invoke-FastPing @("ME0025") -PingCount 1
# write-host "test2"
# Invoke-FastPing @("ME0001") -PingCount 1
# write-host "test3"
function blub($status) {
$status
# $s = $false
# if ($status.Status -eq "RanToCompletion") {
# $s = $true
# }
# $s
}
# $ping = [System.Net.Networkinformation.Ping]::new()
# $ping2 = [System.Net.Networkinformation.Ping]::new()
# $ping3 = [System.Net.Networkinformation.Ping]::new()
# #$run = $ping.SendPingAsync("ME0025", 20000)
# $run2 = $ping2.SendPingAsync("ME-Linux-CP")
# # $run3 = $ping3.SendPingAsync("ME0001")
# # blub -status $run
# blub -status $run2
# # blub -status $run3
$RemoteHosts = @('ME0025', 'ME0001')
# Initiate a Ping asynchronously per remote host, pick up the result task objects
$Tasks = foreach ($ComputerName in $RemoteHosts) {
(New-Object System.Net.NetworkInformation.Ping).SendPingAsync($ComputerName)
}
# Wait for all tasks to finish
[System.Threading.Tasks.Task]::WaitAll($Tasks)
# Output results
$Tasks | Select-Object -Expand Result