Author Posts

June 6, 2016 at 11:17 pm

I have a script that used to work that would enumerate all the members of the local administrators group from a list of workstations.

My network has a mix of Windows 7, 8.1 and 10 enterprise. desktops. I'm running this script from my Windows 10 box.

PS D:\scripts> $PSVersionTable

Name Value
—- —–
PSVersion 5.0.10586.122
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.10586.122
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

Error:

Error while invoking GetType. Could not find member.
At D:\Scripts\GetLocalAdmin2.ps1:22 char:65
+ ... mbers") | %{$_.GetType().InvokeMember("Name", 'GetProperty', $null, $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], MissingMemberException
+ FullyQualifiedErrorId : System.MissingMemberException

Script:

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 {}
}

Get-Content D:\7Alive.txt | get-localusers | Export-Csv D:\localusers.csv -NoTypeInformation

Help?

June 6, 2016 at 11:37 pm

You are missing a loop for every computer in your function.

function Get-LocalUsers {
    param(
        [Parameter(Mandatory=$true,valuefrompipeline=$true)]
        [string]$Computer
    )
    begin {}
    process {
        foreach ($cmp in $Computer) {
            "Do something for {0}" -f $cmp
        }
    }
    end{}
} #function


"Computer1", "Computer2" | Get-LocalUsers

Also, if you are trying to learn, this is a great way. I'm pretty sure there are a lot of scripts out there to get the members of the Local Administrators group, so you can also look up some examples to learn different ways of getting local user information.

June 7, 2016 at 3:15 pm

Here are the changes:

function Get-LocalUsers {
param(
[Parameter(Mandatory=$true,valuefrompipeline=$true)]
[string]$Computer
)
begin {}
process {

foreach ($cmp in $Computer) {
$adminlist =""
$pc = [ADSI]("WinNT://" + $cmp + ",computer")
$AdminGroup = $pc.psbase.children.find("Administrators")
$Adminmembers= $AdminGroup.psbase.invoke("Members") | %{$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
foreach ($admin in $Adminmembers) { $adminlist = $adminlist + $admin + "," }
$pc = New-Object psobject
$pc | Add-Member noteproperty ComputerName $cmp
$pc | Add-Member noteproperty Administrators $adminlist
Write-Output $pc
}
}
end{}
} #function

And still get same error and it doesn't enumerate all the members:

PS D:\scripts> get-localusers -Computer box01
Error while invoking GetType. Could not find member.
At line:13 char:65
+ ... mbers") | %{$_.GetType().InvokeMember("Name", 'GetProperty', $null, $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], MissingMemberException
+ FullyQualifiedErrorId : System.MissingMemberException

Error while invoking GetType. Could not find member.
At line:13 char:65
+ ... mbers") | %{$_.GetType().InvokeMember("Name", 'GetProperty', $null, $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], MissingMemberException
+ FullyQualifiedErrorId : System.MissingMemberException

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

June 7, 2016 at 3:51 pm

This is a better query and it works. Now, I just need to be able to add text file with computer names and output in a clean format.

function Get-LocalUsers {
param(
[Parameter(Mandatory=$true,valuefrompipeline=$true)]
[string]$Computer
)
begin {}
process {
foreach ($cmp in $Computer) {
$group = get-wmiobject win32_group -ComputerName $cmp -Filter "LocalAccount=True AND SID='S-1-5-32-544'"
$query = "GroupComponent = `"Win32_Group.Domain='$($group.domain)'`,Name='$($group.name)'`""
$list = Get-WmiObject win32_groupuser -ComputerName $cmp -Filter $query
$list.PartComponent | % {$_.substring($_.lastindexof("Domain=") + 7).replace("`",Name=`"","\")}
}
}
end{}

The output of this script for one computer looks like this:

"Box01\Administrators"
"Contoso\Domain Admins"
"Contoso\Bill"

Be nice to be able to run this:
get-content D:\Users.txt | get-localusers | export-csv d:\users.csv -notypeinformation

June 7, 2016 at 10:36 pm

if it helps any I have a script i wrote with primal script a few years ago that presents a gui for adding and removing local users from groups on a machine you specify: