Here is the final result:
code:
On Error Resume Next
Dim UserCount, gga, CNgga
gga= inputbox("Which global group?") 'Get the requested Global Group
CNgga = "CN="&gga 'Change the group to a CN request
Set objShell = Wscript.CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOU = GetObject("
LDAP://OU=Application Management Groups,OU=Security Groups,DC=AD,DC=Intra") 'input the full LDAP path to your group's OU here
objDesktop = objShell.SpecialFolders("Desktop") 'Folder for the output file
For Each Group In objOU
If InStr(Group.Name, CNgga) Then 'input the name of the AD group to query here
Set objFile = objFSO.CreateTextFile(objDesktop & "\AD\Members of " & Group.Name & ".txt", 2) 'Create the file
set UserCount = 0
objFile.WriteLine Group.Name
objFile.WriteLine "=================================="
Set objGroup = GetObject ("LDAP://" & Group.Name & ",OU=Application Management Groups,OU=Security Groups,DC=AD,DC=Intra") 'input the full LDAP path to your group's OU here
objGroup.GetInfo
arrMemberOf = objGroup.GetEx("member")
For Each strMember In arrMemberOf
Set objUser = GetObject("LDAP://" & strMember)
Select Case objUser.class
Case "user"
objFile.WriteLine " " & objUser.samaccountname &" - " & objUser.Class
UserCount = UserCount + 1
Case "group"
'objFile.WriteLine objUser.samaccountname &" - " & objUser.Class &" - " & objUser.distinguishedName
enumGroupMembers(objUser.distinguishedName)
End Select
Next
objFile.WriteLine "Aantal gebruikers: " & UserCount 'Gives a total count of all the users.
objFile.Close
End If
Next
MsgBox "Completed script.",64,"Informational"
Sub enumGroupMembers(sObjDN)
Dim oContainer, obj, sDN
Set oContainer=GetObject ("LDAP://" & sObjDN)
For each obj in oContainer.members
Select Case LCase(obj.Class)
Case "user" , "contact"
objFile.WriteLine " " & obj.sAMAccountName &" - " & obj.Class
UserCount = UserCount + 1
Case "group"
'objFile.WriteLine obj.sAMAccountName &" - " & obj.Class &" - " & objUser.distinguishedName
EnumGroupMembers obj.distinguishedName
End Select
Next
End Sub