Powershell

Purpose:

Use as a filter to select computers from a stream on which to act upon later in the pipeline. Example:

get-content servers.txt | select-alive | get-wmiobject `
win32_foo

No output will be given unless you set the -Verbose switch or otherwise have enabled $VerbosePreference.

(Note: This script is in the form of a library. You have to dot-source it into your session, then you can call the function. For more information on dot-sourcing, see this article: Running Windows PowerShell Scripts.)

   1: filter Select-Alive {
   2:     param ( [switch]$Verbose )
   3:     trap {
   4:         Write-Verbose "$(get-date -f 's') ping failed: $cn"
   5:         continue
   6:     }
   7:     if ($Verbose) {
   8:         $VerbosePreference = "continue"
   9:         $ErrorActionPreference = "continue"
  10:     }
  11:     else {
  12:         $VerbosePreference = "silentlycontinue"
  13:         $ErrorActionPreference = "silentlycontinue"
  14:     }
  15:     Write-Verbose "$(get-date -f 's') ping start"
  16:     $ping = New-Object System.Net.NetworkInformation.Ping
  17:     $reply = $null
  18:     $_ | foreach-object {
  19:         $obj = $_
  20:         # Accomodate different input object types
  21:         # thx Gaurhoth (http://thepowershellguy.com/blogs/gaurhoth/archive/2007/10/08/an-example-of-how-to-use-new-taskpool.aspx)
  22:         switch ($obj.psbase.gettype().name) {
  23:             "DirectoryEntry"    { $cn = $obj.dnshostname[0] }
  24:             "IPHostEntry"        { $cn = $obj.HostName }
  25:             "PSCustomObject"    { $cn = $obj.Name }
  26:             "SearchResult"      { $cn = $obj.properties['dnshostname'][0] }
  27:             "String"            { $cn = $obj.trim() }
  28:         }
  29:         Write-Verbose "$(get-date -f 's') pinging $cn..."
  30:         $searchCount++
  31:         $reply = $ping.Send($cn)
  32:         if ($reply.status -eq "Success") {
  33:             $cn; $pingCount++
  34:         }
  35:     }
  36:     Write-Verbose "$(get-date -f 's') ping end - $pingCount/$searchCount online"
  37: }

Download file: lib-tcp.ps1

Thanks to Hons at #PowerShell for his Get-Reachables function of which mine is a modification.

Updated: 12/6 @ 11:01 PM

I’ve added some code I stole from Gaurhoth so that the script can work with different input object types.

: http://halr9000.com/article/447

( Show All | Hide All ) Post a comment now » Sorry, the comments are closed.
2007-11-05 07:48:08

Hi Hal,

I was just looking this filter over and I think it’s missing something (but I don’t have time to test it right now to verify that). Correct me if I’m wrong, but isn’t this script permanently changing the values of $verbosePreference and $errorActionPreference such that the changes are retained after the filter has exited? Wouldn’t it be better to store the current values when you enter the filter, set them based on the switch parameter, and then reset to their original values before you exit? Otherwise you might cause unexpected behaviour in some users environments after they’ve used this filter, no?

Kirk out.

2007-11-05 08:27:45

Thanks for the comments Kirk. No, the default function scope is such that my changes to those global variables don’t stick. At least that’s what my testing has shown. Try it yourself, check the value of those before and after running my function.

Changing subjects, I’m thinking of expanding this function to include an RPC ping for when ICMP isn’t desirable, and maybe an WinRM check as well. Or that might be a separate function.

Jarmo
2008-01-23 02:34:15

Thanks for the script.
I have made some corrections/additions:

Line 4: change $computer to $cn

Line 25(not bug, but a nice addition):

“PSCustomObject” { if ($obj.cn) { $cn = $obj.cn} else { $cn = $obj.Name } }

Lines 32-34: Current script is very quiet, if computer has an ip address, but does not respond, so I added else statement:

if ($reply.status -eq “Success”) {
$cn; $pingCount++
} else {
Write-Verbose “$(get-date -f ’s’) ping $($reply.status): $cn”
}

2008-11-16 22:30:05

[...] how to build functions which can be used in a pipeline. Hal wrote a script a while ago called Select-Alive which serves as a good [...]

Sorry, the comment form is closed at this time.

  • Microblog

  • Recent Posts

  • Recent Comments

  • meta