Link to home
Start Free TrialLog in
Avatar of Jase_x
Jase_x

asked on

Using VBS to find group memberships in Win 2k3 - Slight problem with code

Hi folks I have the following code which I am using to print to screen the groups of which the current logged in user is part of :

On Error Resume Next
Set objADSysInfo = CreateObject("ADSystemInfo")
strUser = objADSysInfo.UserName
Set objUser = GetObject("LDAP://" & strUser)
For Each strGroup in objUser.memberOf
    Set objGroup = GetObject("LDAP://" & strGroup)
    Wscript.Echo objGroup.Name
Next

When a user is part of 3 or more groups the script works perfectly.  However I have one user which is part of the domain users group and a test group.  When you run the script on this user it does not return any groups until you add a third group which can be anything.

I was just wondering if anyone knew why this was the case ? It is not a permissions based thing as I have tried giving the user administrative privilages.

Thanks folks,

- Jamie
Avatar of sr75
sr75
Flag of United States of America image

Try this script out:

<script>

Option Explicit
Dim objRootLDAP, objGroup, objUser, objOU, objmemberOf, objNetwork
Dim strOU, strUser, strDNSDomain, strLDAP, strList

Set ObjNetwork = CreateObject("Wscript.Network")

Set objRootLDAP = GetObject("LDAP://RootDSE")
strDNSDomain = objRootLDAP.Get("DefaultNamingContext")

strUser = ObjNetwork.UserName
strUser ="cn=" & strUser & ","
strOU ="CN=Users,"
strLDAP ="LDAP://" & strUser & strOU & strDNSDomain

Set objUser = GetObject(strLDAP)

objmemberOf  = objUser.GetEx("memberOf")
For Each objGroup in objmemberOf
   strList = strList & objGroup & vbcr
Next

WScript.Echo "Groups for " & strUser & vbCr & strList

WScript.Quit

<end script>
Avatar of Jase_x
Jase_x

ASKER

Thanks for that I shall try that in a second :)

Quick question which is sort of related.

I need to search a string which has phrases seperated by "," such as "cn=test,cn=test2" and when it finds a match to say another string such as "cn=test" it stops and prints a message.  

For instance, the script above provides a mechanisim for showing which groups a user belongs to, I need it to put this list of groups in a string and compare that string against a constant string so that if it finds a match it writes a message ... for instance ...

String1 (Contains all the groups that the user is part of)
String 2 (Contains a set number of groups) such as ... "cn=test,cn=test2,cn=test3"

If string1 matches string2 (any part of it) then it displays a message.

I hope this makes sense ? I know this is pretty easy but I have no idea how to do this in vbs !

Thanks for your help,

- Jamie
ASKER CERTIFIED SOLUTION
Avatar of sr75
sr75
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial