Get-Ryan

[PowerShell] Ping-Hostname

Apr 27th, 2016
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Ping-Hostname {
  2.  
  3.     [cmdletbinding()]
  4.  
  5.     param(
  6.         [parameter(Mandatory=$true,
  7.                    ValueFromPipeline=$true,
  8.                    ParameterSetName="String")]
  9.         [String[]]
  10.         $HostName,
  11.  
  12.         [parameter(Mandatory=$true,
  13.                    ValueFromPipeline=$true,
  14.                    ParameterSetName="Object")]
  15.         [PSObject[]]
  16.         $Computers
  17.         )
  18.  
  19.     Begin{
  20.         $ComputersArray = New-Object System.Collections.ArrayList
  21.     }
  22.  
  23.     Process{
  24.         If($HostName){
  25.             ForEach($Name in $HostName){
  26.                 $Computer = [PSCustomObject]@{
  27.                             Name = $Name
  28.                             Job = (Test-Connection -ComputerName $Name -Count 5 -BufferSize 16 -AsJob)
  29.                             Return = $null
  30.                             ScriptTypeObject = $false #Tag object to return only hostname string in final processing
  31.                             }
  32.                 $ComputersArray.Add($Computer) | Out-Null
  33.             }
  34.         }
  35.         Else{
  36.             ForEach($Computer in $Computers){
  37.                 Add-Member -InputObject $Computer -Name 'Job' -MemberType NoteProperty -Value (Test-Connection -ComputerName $Computer.Name -Count 5 -BufferSize 16 -AsJob) -Force
  38.                 Add-Member -InputObject $Computer -Name 'Return' -MemberType NoteProperty -Value $null -Force
  39.                 Add-Member -InputObject $Computer -Name 'ScriptTypeObject' -MemberType NoteProperty -Value $true -Force #Tag object to return full object in final processing
  40.                 $computersArray.Add($Computer) | Out-Null
  41.             }
  42.         }
  43.     }
  44.  
  45.     End{
  46.         $Result = New-Object System.Collections.ArrayList
  47.  
  48.         Get-Job | Wait-Job | Out-Null
  49.  
  50.         ForEach($Computer in $ComputersArray){
  51.             If($Computer.Return -eq $null){
  52.                 $Computer.Return = Receive-Job $Computer.Job
  53.             }
  54.         }
  55.  
  56.         ForEach($Computer in $ComputersArray){
  57.             If($Computer.Return | %{If($_.StatusCode -eq 0){$true}}){
  58.                 If($Computer.ScriptTypeObject -eq $false){ #Return only strings or full objects based on received input
  59.                     $Result.Add($Computer.Name) | Out-Null
  60.                 }
  61.                 Else{
  62.                     $Computer.PSObject.Members.Remove('Job')
  63.                     $Computer.PSObject.Members.Remove('Return')
  64.                     $Computer.PSObject.Members.Remove('ScriptTypeObject')
  65.                     $Result.Add($Computer) | Out-Null
  66.                 }
  67.             }
  68.         }
  69.         Get-Job | Where-Object {$_.Command -like 'Test-Connection'} | Remove-Job
  70.         $Result
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment