![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||