Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i am using vbscript to read user belongs to which group:

strUsers = inputbox("Enter user name")

'**************************************************************************
'*****                DO NOT EDIT BEYOND THIS POINT                   *****
'**************************************************************************


arrUsers = Split(strUsers, ",")
Set fs = CreateObject ("Scripting.FileSystemObject")
Set outFile = fs.CreateTextFile (".\User Group Membership Report.txt")

outFile.WriteLine
outFile.WriteLine "                       User Group Membership Report"
outFile.WriteLine "                       ----------------------------"
outFile.WriteLine
outFile.WriteLine "Run on: " & Now()
For x = 0 to UBound(arrUsers)
	arrUsers(x) = Trim(arrUsers(x))
	If x = 0 Then
		outFile.WriteLine " Users: " & arrUsers(x)
	Else
		outFile.WriteLine "        " & arrUsers(x)
	End If
Next 
outFile.WriteLine "=================================================================="
outFile.WriteLine
For x = 0 to UBound(arrUsers)
	Call getGroups(arrUsers(x))
	outFile.WriteLine "------------------------------------------------------------------"
Next

Wscript.echo "Done!"


Sub getGroups(strUser)
	Set oNetwork = CreateObject("WScript.Network")
	strDomain = oNetwork.UserDomain
	On Error Resume Next
	Set oUser = GetObject("WinNT://" & strDomain & "/" & strUser & ",user")
	If Err.Number <> 0 Then
		outFile.WriteLine strUser & " not found..."
		Exit Sub
	End If
	Err.Clear
	On Error Goto 0
	
	outFile.WriteLine strUser & " - " & oUser.FullName

	For Each strGroup In oUser.Groups
		outFile.WriteLine "     " & strGroup.Name
            
	Next
Set GroupCollection = oUser.Groups
	WScript.Echo "User " & StrUserName & " is a Member of all the following Groups:"
	For Each ObjGroup In GroupCollection
		outFile.WriteLine "  >> " & Trim(ObjGroup.Name)
		' -- Now List all Nested Groups of which the User is a Member
		GetNested ObjGroup.Name
	Next
End Sub
Function GetNested(objGroup)
    On Error Resume Next
outFile.WriteLine "  checking for nested group>> "
   Set objSysInfo = CreateObject("ADSystemInfo")
   strUser = objSysInfo.UserName
   Set objUser = GetObject("LDAP://" & strUser)
    For Each strMember in objUser
        strPath = "LDAP://" & strMember
        Set objNestedGroup = GetObject(strPath)
        strGroupList = strGroupList & "[" + objNestedGroup.Name & "] "
outFile.WriteLine "  >> " & strGroupList
        GetNested(objNestedGroup)
    Next
End Function


i am getting groups but not nested groups..please help.

What I have tried:

i tried with LDAP its worked,but its unable to read some of groups,so i decided to go with winnt// .so i am able to read groups but not nested groups
Posted
Updated 25-Oct-17 22:15pm
v2
Comments
CHill60 26-Oct-17 5:49am    
You're not returning anything from the GetNested Function for starters. Try getting rid of the "On Error Resume Next" and the error message might help you find out what's wrong
lakshjoshi 27-Oct-17 3:43am    
ok..thanks for help.i will check it

1 solution

finally i got solution to read nested group.

'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
  '  Wscript.Echo objGroup.CN
   ' GetNested objGroup

'Next
dim temp
Set d = CreateObject("Scripting.Dictionary")
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_INITTYPE_GC = 3
  Const ADS_NAME_TYPE_1779 = 1
  Const ADS_NAME_INITTYPE_SERVER = 2
  Const ADS_NAME_INITTYPE_DOMAIN = 1
Wscript.Echo "==================================================="
Set objNetwork = WScript.CreateObject("WScript.Network")
'strUserName =objNetwork.Username
strUserName ="laxjoshi"
wscript.echo strUserName
strComputerName = objNetwork.ComputerName
strUserDomain = objNetwork.UserDomain
Set objUser = GetObject("WinNT://" & strUserDomain & "/" & strUserName & ",user")
strGroupList = ""
For Each objGroup In objUser.Groups
    'wscript.echo objGroup.Name
strGroupName=objGroup.Name
Set objTrans = CreateObject("NameTranslate")
objTrans.Init ADS_NAME_INITTYPE_DOMAIN, strUserDomain
	strNTName = strUserDomain & "\" & strGroupName
       objTrans.Set ADS_NAME_TYPE_NT4, strNTName
        strGroupDN = objTrans.Get(ADS_NAME_TYPE_1779)
  ' Escape any forward slash characters, "/", with the backslash
  ' escape character. All other characters that should be escaped are.
       strGroupDN = Replace(strGroupDN, "/", "\/")
'wscript.echo "strgroup="& strGroupDN 
 ' strGroupDN = "LDAP://" & strGroupDN
'wscript.echo "strdn="& strGroupDN 

Set objGroup = GetObject("LDAP://" & strGroupDN)
    'Wscript.Echo objGroup
    Wscript.Echo objGroup.CN
   strgroupname=objGroup.CN
'If IsMember(strUserDomain,strUserName,strgroupname) Then
 '   Wscript.echo "The user '"& strUserName &"' exists in the group '"&strgroupname&"'"
 'Else
  '  Wscript.echo "The user '"&strUserName&"' not exists in the group '"&strgroupname&"'"
'End If

   'd.add objGroup.CN
    GetNested objGroup




  'adsPathNT2DN = strGroupDN 
 'Set ngrp = GetObject("WinNT://" & strUserDomain & "/" & objGroup.Name & ",group")
'For Each member In ngrp.Members
'wscript.echo member.name
'If (StrComp(UCase(member.Class), "GROUP") = 0) Then
'wscript.echo "Group name="& member.name
'end if
'Next
'wscript.echo "=================================================="
next
wscript.echo "=================================================="

'If IsMember(strUserDomain,strUserName,State) Then
 '   Wscript.echo "The user '"& strUserName &"' exists in the group '"&State&"'"
 'Else
  '  Wscript.echo "The user '"&strUserName&"' not exists in the group '"&State&"'"
'End If
'*************************************************************************

  
' *****************************************************
'This function checks if the given AD user is member of the given group.
Function IsMember(domainName,userName,groupName)
   Set groupListD = CreateObject("Scripting.Dictionary")
   groupListD.CompareMode = 1
   ADSPath = domainName & "/" & userName
   Set objUser = GetObject("WinNT://" & ADSPath & ",user")
   For Each objGroup in objUser.Groups
      groupListD.Add objGroup.Name, "-"
   Next
   IsMember = CBool(groupListD.Exists(groupName))
End Function
'******************************************************************************
'******************************************
Function GetNested(objGroup)
    On Error Resume Next
    colMembers = objGroup.GetEx("memberOf")
    For Each strMember in colMembers

        strPath = "LDAP://" & strMember
        Set objNestedGroup = GetObject(strPath)
        strGroupList =  objNestedGroup.CN
      if  strGroupList <> "" and temp <> strGroupList then
        temp=strGroupList
	'strGroupList = Replace(strGroupList, "CN= ", "")
	strGroupList=trim(strGroupList)
        d.add strGroupList
	wscript.echo ">>>>>"& strGroupList 	
     End If
         
        GetNested(objNestedGroup)
    Next
End Function
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900