Get-Ryan

[PowerShell] Test-PCConnection

Apr 18th, 2017
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     Function Test-PCConnection {
  2.         [cmdletbinding()]
  3.         param(
  4.             [parameter(Mandatory = $true,
  5.                 ValueFromPipeline = $true,
  6.                 ValueFromPipelineByPropertyName = $true)]
  7.             [String[]]
  8.             $ComputerName
  9.             )
  10.      
  11.         Begin{
  12.             $ComputersArray = New-Object System.Collections.Generic.List[object]
  13.         }
  14.      
  15.         Process{
  16.             ForEach($name in $ComputerName){
  17.                 $ComputersArray.Add(
  18.                     [PSCustomObject]@{
  19.                         ComputerName = $name
  20.                         Job = (Test-Connection -ComputerName $name -Count 3 -BufferSize 16 -AsJob)
  21.                         Return = $null
  22.                         PingResult = $null})
  23.             }
  24.         }
  25.      
  26.         End{
  27.             [void](Get-Job | Wait-Job)
  28.      
  29.             ForEach($computer in $ComputersArray){
  30.                 $computer.Return = Receive-Job $computer.Job
  31.                 If($computer.Return | ForEach-Object {If($_.StatusCode -eq 0){$true}}){
  32.                     $computer.PingResult = "Online"
  33.                 }
  34.                 Else{
  35.                     $computer.PingResult = "Offline"
  36.      
  37.                 }
  38.                 $computer.PSObject.Members.Remove('Job')
  39.                 $computer.PSObject.Members.Remove('Return')
  40.             }
  41.      
  42.             Get-Job -Filter @{Command = "Test-Connection"} | Remove-Job
  43.      
  44.             $ComputersArray
  45.         }
  46.     }
Advertisement
Add Comment
Please, Sign In to add comment