PoshCode Archive  Artifact Content

Artifact 2b977adc9fc94de3a55dbae1d8bd97c4f76161455c2be1f4a3e58a3183e97eae:

  • File Select-Alive.ps1 — part of check-in [960df5300b] at 2018-06-10 14:05:57 on branch trunk — Selects only objects containing a hostname that is pingable. (user: unknown size: 911)

# encoding: ascii
# api: powershell
# title: 
# description: Selects only objects containing a hostname that is pingable.
# version: 0.1
# type: function
# license: CC0
# function: Select-Alive
# x-poshcode-id: 601
# x-archived: 2014-08-01T18:21:03
#
#
function Select-Alive {param(	[object]$InputObject,
								[string]$Property,
								[int32]$Requests = 3)

	PROCESS {
		if ($InputObject -eq $null) {$In = $_} else {$In = $InputObject}
		if ($In.GetType().Name -eq "String") {
			$HostName = $In
		} 
		elseif (($In | Get-Member | Where-Object {$In.Name -eq $Property}) -ne $null) {
			$HostName = $In.$Property
		} else {return $null}
		
		for ($i = 1; $i -le $Requests; $i++) {
			$Result = Get-WmiObject -Class Win32_PingStatus -ComputerName . -Filter "Address='$HostName'"
			Start-Sleep -Seconds 1
			if ($Result.StatusCode -ne 0) {return $null}
		}
		return $In
	}
}