Get-Ryan

[PowerShell] Get-OnlineComputers

Oct 7th, 2015
2,312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Get-OnlineComputers() {
  2.  
  3.     param(
  4.         [switch]
  5.         $AllProperties
  6.         )
  7.    
  8.     $pingTest = $null
  9.  
  10.     If($AllProperties){
  11.         $computers = Get-ADComputer -Filter {Enabled -eq $true} -Property * -SearchBase "<SearchBase OU>" -SearchScope Subtree
  12.     }
  13.     Else {
  14.         $computers = Get-ADComputer -Filter {Enabled -eq $true} -SearchBase "<SearchBase OU>" -SearchScope Subtree
  15.     }
  16.    
  17.     ForEach($computer in $computers) {
  18.  
  19.         $pingTest = Test-Connection -ComputerName $computer.Name -Count 1 -BufferSize 16 -AsJob
  20.         $computer | Add-Member -NotePropertyName "PingJob" -NotePropertyValue $pingTest -Force
  21.         $computer | Add-Member -NotePropertyName "PingReturn" -NotePropertyValue $null -Force
  22.  
  23.     }
  24.  
  25.     Get-Job | Wait-Job | Out-Null
  26.  
  27.     ForEach($computer in $computers){
  28.  
  29.         If($computer.PingReturn -eq $null){
  30.  
  31.             $computer.PingReturn = Receive-Job $computer.PingJob
  32.  
  33.         }
  34.  
  35.     }
  36.  
  37.     Get-Job | Remove-Job
  38.     $computers = $computers | Where-Object {$_.PingReturn.StatusCode -eq 0} | Select-Object -Property * -ExcludeProperty PingJob,PingReturn
  39.     $computers
  40. }
Advertisement
Add Comment
Please, Sign In to add comment