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