Advertisement
Guest User

Get-Pingables

a guest
Oct 16th, 2018
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. Function Get-Pingables {
  2. [cmdletbinding()]
  3. Param(
  4. [parameter(Mandatory=$True, ValueFromPipeline = $True, Position = 0)]
  5. [String[]]$ComputerName,
  6. [switch]$Online
  7. )
  8.  
  9. $pingable = [System.Collections.Generic.List[PSObject]]::new()
  10. $ComputerName | ForEach-Object {
  11. Set-Variable -Name "Status_$_" -Value (Test-Connection -ComputerName $_ -AsJob -Count 1)
  12. }
  13.  
  14. Get-Variable "Status_*" -ValueOnly | ForEach-Object {
  15. $Status = Wait-Job $_ | Receive-Job
  16. $reachable = $False
  17. $addr = ""
  18.  
  19. if ($Status.ResponseTime -ne $null ) {
  20. $addr = $Status.Address
  21. $reachable = $True
  22. }
  23.  
  24. $pingable.add([PSCustomObject]@{'Computer' = $Status.Address; 'Online' = $reachable})
  25. }
  26. if ($Online) {
  27. Return $pingable | Where-Object { $_.Online } | Select-Object Computer
  28. } else {
  29. Return $pingable | Sort-Object -Descending -Property 'Online'
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement