Filtering on the Pipeline using Functions

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
b0n3z
Posts: 10
Joined: Fri Mar 21, 2008 9:39 am

Filtering on the Pipeline using Functions

Post by b0n3z » Tue May 13, 2008 4:31 am

Could someone please look at my code and give me a sanity check?

After reading Windows PowerShell
: The Power of Filtering by Don (great article), I thought I would try and add some code to help a task I wanted to accomplish. I wanted to ping a list of workstations in list.txt then check the local administrators group on each workstation.

I wanted to make each function separate to where I could add or remove multiple functions to filter the list of workstations on the pipeline.

What is strange is when I run this code - it shows the following:
WORKSTATION1 AdminGroup is correct
WORKSTATION1 ping success
WORKSTATION2 AdminGroup is correct

WORKSTATION2 ping success

Why is it showing the output of Function Filter-AdminGroup first? If there is a better way I should be doing the each function?

thanks,

Ken

Code: Select all

Function Filter-Ping {
# ping address and filter only those that respond
    PROCESS {
        $ping = $false
        $results = Get-WmiObject -query `
            "SELECT * FROM Win32_PingStatus WHERE Address = '$_'"
        foreach ($result in $results) {
          if ($results.StatusCode -eq 0) {
            $ping = $true
          }
        }
        if ($ping -eq $true) {
             Write-Output $_
             Write-Host "$_ ping success"
        }
        else{
            Write-Host "$_ warning failed ping"
        }
    }
}

Function Filter-AdminGroup{
# check the administrators group on workstation for the following:
# "Domain Admins" or "ADMIN Systems Management"
    PROCESS {
        $admgrp = [ADSI]"WinNT://$_/Administrators,group"

#         Line below needs to be changed to accept "access is denied" or
#         "The network path was not found."
        $admingroup = $admgrp.psbase.invoke("members") |
        ForEach-Object{$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}

        [boolean]$member = $false
        switch ($admingroup) {
            "Domain Admins" {$member = $true}
            "ADMIN Systems Management" {$member = $true}
            default {}
        }

        if ($member -eq $true) {
#             Write-Output $_
            Write-Host "$_ AdminGroup is correct"
        }
        else {
            Write-Host "$_ Error in AdminGroup!"
        }
    }
}

Get-Content "D:powershelllist.txt" | Filter-Ping | Filter-AdminGroup

User avatar
b0n3z
Posts: 10
Joined: Fri Mar 21, 2008 9:39 am

Filtering on the Pipeline using Functions

Post by b0n3z » Wed Jun 04, 2008 1:04 am

thanks for the response jhicks.I wanted to use the Write-Host in each function to give some sort of status where the script is at (running at the command line). This shouldn't be a problem if the Write-Host doesn't affect what's in the Pipeline.

User avatar
jhicks
Posts: 1789
Joined: Sun Jan 21, 2007 11:31 pm

Filtering on the Pipeline using Functions

Post by jhicks » Wed Jun 04, 2008 5:34 am

That's exactly how to use it and when used in pipeline you get the type of result you initially posted. Personally, when I use Write-Host like that I have it display in a different color so I can tell what is coming from the pipeline and what is coming directly to the host.

Locked