Exclude members from GetType InvokeMember in script

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Dan Potter Dan Potter 4 months, 4 weeks ago.

  • Author
    Posts
  • #46987
    Profile photo of Rocky Cabral
    Rocky Cabral
    Participant

    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

  • #46989
    Profile photo of Don Jones
    Don Jones
    Keymaster

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

  • #46991
    Profile photo of Rocky Cabral
    Rocky Cabral
    Participant

    Perfect!

    Thanks Don.

  • #46993
    Profile photo of Dan Potter
    Dan Potter
    Participant

    I don't like it.

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

You must be logged in to reply to this topic.