WMI - list groups a user is member of

marcvs

Member
Joined
Mar 24, 2007
Messages
9
Programming Experience
1-3
Hi All,

I am using WMI to retrieve some user account info. Getting users is easy using the query:
VB.NET:
"SELECT * FROM Win32_UserAccount"

Getting the groups that a user is member of is a bit trickier, eg:

user: Joe
member of: Administrators, Users

I'm using the following query, where myDomain is the Machine Name and theUser is the User Name:
VB.NET:
	"SELECT * FROM Win32_GroupUser " + _
	"WHERE PartComponent = ""Win32_UserAccount.Domain='" + _
	myDomain + "',Name='" + theUser + "'"""
I've seen this in a number of examples but it's not working for me. The query is accepted but seem to return nothing. Any Hints?

Thanks
 
ADSI (similar to WMI; Active Directory Service Interface) seems more appropriate.

ADSI code:
VB.NET:
DomainString="myDomain"
UserString = "myUser"

Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)
    
    For Each GroupObj In UserObj.Groups
        List = List & GroupObj.Name & VbCrLf
    Next

Wscript.Echo List

UserObj = Nothing
GroupObj = Nothing

I am not sure of the exact VB code but try this link [LINK] http://msdn2.microsoft.com/en-us/library/aa705907.aspx [/LINK]; and I will try to have a VB solution by tomorrow afternoon
 
re: WMI user group

Thanks for your interest, TBryce!

ADSI - to be honest I've never heard of that. Do you think I will be able to query remote machines on my LAN with that? Basically, that's the idea of what I'm trying to do here - collect a bunch of info remotely. Something like Everest but with the ability to get info for remote machines.

Another thing - Active Directory. That sounds like Windows Domain Controller "jurisdiction":). Will this let me query machines which are on a workgroup (i.e. not part of a domain)?


Looking forward for more great tips!
Mark
 
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click

        'either hard code or obtain from variable the computer name/user
        Dim DomainString As String = "myDomain"
        Dim UserString As String = "myUser"

        'just set the user/group object to object 
        Dim UserObj, GroupObj As Object
        Dim List As String

        'specify what object to obtain
        UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)

       'enumerate the objects
        For Each GroupObj In UserObj.Groups
            List = List & GroupObj.Name & vbCrLf
        Next

        rtbWMIQueriesCont.Text = List

        UserObj = Nothing
        GroupObj = Nothing

End Sub

for the domain you can just enter the name of the pc in the workgroup... as long as you can browse the pc in the workgroup, then you should be able to obtain the information.
 
Thanks for your interest, TBryce!
Something like Everest but with the ability to get info for remote machines.
Mark

Looks like a great product. I have been working on something similar that is specific to my company. But I would be willing to share the source code and what it does.

It doesn't have all the polish that Everest has, I am shooting for functionality first.
 
Back
Top