Author Posts

July 14, 2016 at 5:31 pm

I have a function that enumerates the members of the local admin group on a remote computer. Now, I would like to clean up the results by excluding common members such as; "Administrator" and "Domain Admins".

How do I exclude those members in the function?

function get-localusers {
param(
[Parameter(Mandatory=$true,valuefrompipeline=$true)]
[string]$StrComputer)
begin {}
Process {
$adminlist =""
$computer = [ADSI]("WinNT://" + $StrComputer + ",computer")
$AdminGroup = $computer.psbase.children.find("Administrators")
$Adminmembers= $AdminGroup.psbase.invoke("Members") | %{$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
foreach ($admin in $Adminmembers) { $adminlist = $adminlist + $admin + "," }
$Computer = New-Object psobject
$computer | Add-Member noteproperty ComputerName $StrComputer
$computer | Add-Member noteproperty Administrators $adminlist
Write-Output $computer

}
end {}
}

PS C:\> get-localusers -StrComputer box01

ComputerName Administrators
———— ————–
box01 Administrator,Domain Admins,jsmith

July 14, 2016 at 5:44 pm

foreach ($admin in $Adminmembers) {
if ( -not ( $admin -in @('Administrator','Joe','Whoever') ) ) {
$adminlist = $adminlist + $admin + ","
}
}

July 14, 2016 at 5:57 pm

Perfect!

Thanks Don.

July 14, 2016 at 6:10 pm

I don't like it.

(@(([ADSI]"WinNT://$computer/administrators").psbase.Invoke("Members")) |%{$_.GetType().Invoke
Member("Name", 'GetProperty', $null, $_, $null)}) -notmatch 'domain admins'