Last active
November 5, 2016 19:37
-
-
Save Graham-Beer/0baf69b575fe8d1f6e90c7be90b0e0ef to your computer and use it in GitHub Desktop.
Ping tool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Get-PingStatus | |
{ | |
param( | |
[Parameter(ValueFromPipeline=$true)] | |
[string]$device, | |
[validateSet("Online","Offline","ObjectTable")] | |
[String]$getObject | |
) | |
begin{ | |
$hash = @() | |
} | |
process{ | |
$device| foreach { | |
if (Test-Connection $_ -Count 1 -Quiet) { | |
if(-not($GetObject)){write-host -ForegroundColor green "Online: $_ "} | |
$Hash = $Hash += @{Online="$_"} | |
}else{ | |
if(-not($GetObject)){write-host -ForegroundColor Red "Offline: $_ "} | |
$Hash = $Hash += @{Offline="$_"} | |
} | |
} | |
} | |
end { | |
if($GetObject) { | |
$Global:Objects = $Hash | foreach { [PSCustomObject]@{ | |
DeviceName = $_.Values| foreach { "$_" } | |
Online = $_.Keys| where {$_ -eq "Online"} | |
offline = $_.Keys| where {$_ -eq "Offline"} | |
} | |
} | |
Switch -Exact ($GetObject) | |
{ | |
'Online' { $Global:Objects| where 'online'| select -ExpandProperty DeviceName } | |
'Offline' { $Global:Objects| where 'offline'| select -ExpandProperty DeviceName } | |
'ObjectTable' { return $Global:Objects } | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment