Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Enumerating local group membership with Powershell

Reply
 
Old 07-05-2007   #1 (permalink)
Janssen


 
 

Enumerating local group membership with Powershell

Hello,

I'm having trouble enumerating local group membership with Powershell. I've
found several VBScript examples, but they never seem to work when I port them
to Powershell. Here's my starting code:
$strSrv=[System.Environment]::MachineName
$strGrp= "Administrators"
$objSrv = New-Object System.DirectoryServices.DirectoryEntry("WinNT://" +
$strSrv + "/Administrators,group")
$members=$objSrv.Members

$objSrv is a valid object, which I can check by typing $objSrv.Name, which
returns:
Administrators

However, $objserv.Members returns a bunch of blank spaces. I've tried
psbase, psadapted, and psextended. None seem to have children or members.
When I try things like $members.GetType(), I get:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False __ComObject
System.MarshalByRefObject

and $members | % {$_.gettype()} returns:

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False __ComObject
System.MarshalByRefObject
True False __ComObject
System.MarshalByRefObject
True False __ComObject
System.MarshalByRefObject

It looks like they are there. I just can't figure out how to display them
on the screen.

Any ideas?

Any help would be much appreciated.

Thanks!

Janssen

My System SpecsSystem Spec
Old 07-05-2007   #2 (permalink)
Janssen


 
 

RE: Enumerating local group membership with Powershell

One other thing, just to clarify. I know that a simple "net localgroup
Administrators" would give me all the info I need. The issue is that I want
to gather this data about 100s of remote machines, and I don't want to use
psexec. I just want a way to get the contents of the "Administrators" groups
on all machines. If there's an eaiser way, I'd be happy to drop the code
below.

Thanks again,

Janssen

"Janssen" wrote:

> Hello,
>
> I'm having trouble enumerating local group membership with Powershell. I've
> found several VBScript examples, but they never seem to work when I port them
> to Powershell. Here's my starting code:
> $strSrv=[System.Environment]::MachineName
> $strGrp= "Administrators"
> $objSrv = New-Object System.DirectoryServices.DirectoryEntry("WinNT://" +
> $strSrv + "/Administrators,group")
> $members=$objSrv.Members
>
> $objSrv is a valid object, which I can check by typing $objSrv.Name, which
> returns:
> Administrators
>
> However, $objserv.Members returns a bunch of blank spaces. I've tried
> psbase, psadapted, and psextended. None seem to have children or members.
> When I try things like $members.GetType(), I get:
> IsPublic IsSerial Name BaseType
> -------- -------- ---- --------
> True False __ComObject
> System.MarshalByRefObject
>
> and $members | % {$_.gettype()} returns:
>
> IsPublic IsSerial Name BaseType
> -------- -------- ---- --------
> True False __ComObject
> System.MarshalByRefObject
> True False __ComObject
> System.MarshalByRefObject
> True False __ComObject
> System.MarshalByRefObject
>
> It looks like they are there. I just can't figure out how to display them
> on the screen.
>
> Any ideas?
>
> Any help would be much appreciated.
>
> Thanks!
>
> Janssen

My System SpecsSystem Spec
Old 07-05-2007   #3 (permalink)
Brandon Shell


 
 

Re: Enumerating local group membership with Powershell

Here is the function I use.

function Check-GroupMembers{
Param([string]$group,[string]$server,[string]$user)
If(!($server)){$server = get-content env:COMPUTERNAME}
$g = [ADSI]("WinNT://$server/$group,group")
$ulist = $g.psbase.invoke("Members") |
%{$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
if($user)
{
foreach($u in $ulist)
{
if($u -eq $user){$found = $true}
}
if($found){Write-Host "User [$user] Found" -ForegroundColor
green;$true}
else{Write-Host "User [$user] NOT found!" -ForegroundColor
red;$false}}
else{$ulist}
}

"Janssen" <Janssen@discussions.microsoft.com> wrote in message
news:9D66A50F-8B6A-4E0F-B9AC-2BDB678E9E93@microsoft.com...
> One other thing, just to clarify. I know that a simple "net localgroup
> Administrators" would give me all the info I need. The issue is that I
> want
> to gather this data about 100s of remote machines, and I don't want to use
> psexec. I just want a way to get the contents of the "Administrators"
> groups
> on all machines. If there's an eaiser way, I'd be happy to drop the code
> below.
>
> Thanks again,
>
> Janssen
>
> "Janssen" wrote:
>
>> Hello,
>>
>> I'm having trouble enumerating local group membership with Powershell.
>> I've
>> found several VBScript examples, but they never seem to work when I port
>> them
>> to Powershell. Here's my starting code:
>> $strSrv=[System.Environment]::MachineName
>> $strGrp= "Administrators"
>> $objSrv = New-Object System.DirectoryServices.DirectoryEntry("WinNT://" +
>> $strSrv + "/Administrators,group")
>> $members=$objSrv.Members
>>
>> $objSrv is a valid object, which I can check by typing $objSrv.Name,
>> which
>> returns:
>> Administrators
>>
>> However, $objserv.Members returns a bunch of blank spaces. I've tried
>> psbase, psadapted, and psextended. None seem to have children or
>> members.
>> When I try things like $members.GetType(), I get:
>> IsPublic IsSerial Name BaseType
>> -------- -------- ---- --------
>> True False __ComObject
>> System.MarshalByRefObject
>>
>> and $members | % {$_.gettype()} returns:
>>
>> IsPublic IsSerial Name BaseType
>> -------- -------- ---- --------
>> True False __ComObject
>> System.MarshalByRefObject
>> True False __ComObject
>> System.MarshalByRefObject
>> True False __ComObject
>> System.MarshalByRefObject
>>
>> It looks like they are there. I just can't figure out how to display
>> them
>> on the screen.
>>
>> Any ideas?
>>
>> Any help would be much appreciated.
>>
>> Thanks!
>>
>> Janssen


My System SpecsSystem Spec
Old 07-05-2007   #4 (permalink)
Jacques Barathon [MS]


 
 

Re: Enumerating local group membership with Powershell

"Janssen" <Janssen@discussions.microsoft.com> wrote in message
news:9D66A50F-8B6A-4E0F-B9AC-2BDB678E9E93@microsoft.com...
> One other thing, just to clarify. I know that a simple "net localgroup
> Administrators" would give me all the info I need. The issue is that I
> want
> to gather this data about 100s of remote machines, and I don't want to use
> psexec. I just want a way to get the contents of the "Administrators"
> groups
> on all machines. If there's an eaiser way, I'd be happy to drop the code
> below.


See the get-groupmember script on my blog entry:
http://janel.spaces.live.com/blog/cn...88C2!300.entry

The blog is in French but you should be able to understand how the script
works. Let me know if you have any question.

Speaking of querying the Administrators group of hundreds of servers
remotely, if you have to deal with different system languages you will also
be interested in a simple way to query the "Administrators" group on those
systems whatever the local group name is:
http://janel.spaces.live.com/blog/cn...ry?_c=BlogPart

Hope that helps,
Jacques

My System SpecsSystem Spec
Old 07-05-2007   #5 (permalink)
Janssen


 
 

Re: Enumerating local group membership with Powershell

Brandon and Jacques,

Thanks so much for your help!!!

$member.GetType().InvokeMember("AdsPath","GetProperty",$null,$member,$null)
was the magic syntax. I have to say, I would never have guessed that. After
trying $member | GM, $member.psbase | GM, $member.children, $member.members,
etc., I was giving up. Maybe there will be a built-in commandlet for this
basic function in v2.

Thanks again for this valuable information,
Janssen
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Enumerating user's universal groups membership from ANOTHER DOMAIN VB Script
Get group membership PowerShell
Enumerating user's universal groups membership from ANOTHER DOMAIN VB Script
Group Membership Vista mail
Enumerating group membership & ADSI PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46