Recently I posted a function to get information about local user accounts. I received a lot of positive feedback so it seemed natural to take this the next step and create a similar function to enumerate or list members of a local group, such as Administrators.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
The function, Get-LocalGroupMember, also relies on ADSI and is similar to my local user function so I won't repeat the details. Since I assume most of the time the only local group that matters is Administrators I made that the default. I also set the computer default to the local host. But it is simple enough to query another computer.
You can pipe in computer names and use the object properties to do additional filtering, sorting or grouping.
"chi-fp02","chi-core01","chi-hvr1","chi-hvr2","chi-web02" | get-localgroupmember | where {$_.IsLocal -AND $_.name -ne 'Administrator'} | Select Computername,Class,Name
In this example I wanted to search a group of computers and identify local members of Administrators that were not the Administrator account.
As with my previous function, I think you'll find it better to use PowerShell remoting if you plan on querying multiple remote servers or need to use alternate credentials. You can read function help for more details.
You can always find the most current version of the function in my Github library.
I hope you find this useful. Comments are welcome here, but please post bugs or suggestions on GitHub.
Get-LocalGroupMember -Computername “mycomputerN”
foreach : Error while invoking GetType. Could not find member.
At line:176 char:39
+ $group.psbase.invoke(“Members”) | foreach {
+ ~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [ForEach-Object], MissingMemberException
+ FullyQualifiedErrorId : System.MissingMemberException,Microsoft.PowerShell.Commands.ForEachObjectCommand
That is odd. I would expect if there was an error connecting to the computer or group you wouldn’t get that far. Are you querying a group on the local computer or a remote? What version of PowerShell is running?
If I run this and enter “Administrators” for the group name it errors out. The group contains Administrator and my user account (2 users). The error is below. Let me know if I overlooked something when trying to run it?
foreach : Error while invoking GetType. Could not find member.
At C:\Scripts\PowerShell\Get-LocalGroupMember.ps1:146 char:39
+ $group.psbase.invoke(“Members”) | foreach {
+ ~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [ForEach-Object], MissingMemberException
+ FullyQualifiedErrorId : System.MissingMemberException,Microsoft.PowerShell.Commands.ForEachObjectCommand
What version of PowerShell are you running this on? What is the OS on the remote computer?
The only other thing I can think of is that there is some sort of timeout happening trying to invoke the COM objects. Does the command work on the local computer?
Since the original post, I’ve updated the function on GitHub to v1.5. Hoping it handles these unusual errors better although I’m still not sure what is causing them.
Or take a look at version 2 http://bit.ly/1QmYFQO