[Win32utils-devel] Getting a list of groups for a given user

Berger, Daniel Daniel.Berger at qwest.com
Tue Mar 3 15:50:27 EST 2009


 

> -----Original Message-----
> From: win32utils-devel-bounces at rubyforge.org 
> [mailto:win32utils-devel-bounces at rubyforge.org] On Behalf Of 
> Daniel Berger
> Sent: Sunday, March 01, 2009 10:54 PM
> To: Development and ideas for win32utils projects
> Subject: [Win32utils-devel] Getting a list of groups for a given user
> 
> Hi,
> 
> How does this look for finding a list of groups for a particular user?
> 
> This is for the sys-admin library. Assume "self.name" is 
> pretty much the same thing as Etc.getlogin would return.
> 
> # admin.rb
> Class User
>    def groups(domain=nil)
>          host = Socket.gethostname
>          cs = "winmgmts:{impersonationLevel=impersonate}!"
>          cs << "//#{host}/root/cimv2"
> 
>          begin
>             wmi = WIN32OLE.connect(cs)
>          rescue WIN32OLERuntimeError => err
>             raise Error, err
>          end
> 
>          if domain
>             query = " select * from Win32_GroupUser where 
> partcomponent = "
>             query <<
> "\"Win32_UserAccount.Domain='#{domain.upcase}',Name='#{self.name}'\""
>          else
>             query = " select * from Win32_GroupUser where 
> partcomponent = "
>             query <<
> "\"Win32_UserAccount.Domain='#{host.upcase}',Name='#{self.name}'\""
>          end
> 
>          array = []
> 
>          wmi.execquery(query).each{ |groupuser|
>             array << 
> groupuser.GroupComponent.split('=').last.tr('"', '')
>          }
> 
>          array
>       end
> end

Nevermind this. It was easier to use the IADsUser interface:

adsi = WIN32OLE.connect("WinNT://some_domain/some_user")
adsi.groups.each{ |g| p g.name }

Regards,

Dan


More information about the win32utils-devel mailing list