VB Script returns all group memberships for a user EXCEPT Exchange Dist groups
I have a VB script which queries AD and returns all the groups a user is a member of. This works great, except as it turns out, it is only returning the groups which are "security" groups, and not "distribution" groups. Not sure what the difference here would be from the investigations I've done using LDIFDE.
Any help would be appreciated
Thanks
RE: VB Script returns all group memberships for a user EXCEPT Exchange
Could you pelase post the script so that we can have a look at it?
Re: VB Script returns all group memberships for a user EXCEPT Exchange Dist groups
Incase you are using the tokenGroups attributem this never includes any distributing groups. It will just include all nested group memberships, and the primary group, but only security groups. The other method I can think of might include distributing groups.
Re: VB Script returns all group memberships for a user EXCEPT Exchange Dist groups
Here is the script in question
Option Explicit
On Error Resume Next
Const FileRead = 1
Const FileWrite = 2
Const OverwriteExisting = True
Dim ObjUser, objFso, WriteToFile, strDomain, member, strUsrName, objWshShell, strCurrenttime, strFileName
strUsrName = inputbox ("User Name", "Display User Group Membership")
strCurrenttime = Now()
strDomain = "MyDomain"
Set ObjUser = Getobject("WinNT://" & strDomain & "/" & strUsrName & ",user")
set objFso = CreateObject("Scripting.FileSystemObject")
objFso.CreateTextFile(strUsrName & ".txt")
set WriteToFile = objFso.OpenTextFile(strUsrName & ".txt", FileWrite, True)
writetofile.write "These are the group memberships for " & strUsrName & vbcrlf
writetofile.write "Memberships as of " & strCurrenttime & vbcrlf & vbcrlf
writetofile.write "Groupname" & vbcrlf & vbcrlf
For Each Member in ObjUser.Groups
WriteToFile.write member.name & vbcrlf
next
WriteToFile.close
'objFSO.CopyFile strUsrName & ".txt" , "\\MyDomain\files\public\temp", OverwriteExisting
objFSO.deleteFile "\\MyDomain\files\IT\Support\Staff Change Requests\GroupMembershipQueries\" & strUsrName & ".txt"
objFSO.MoveFile strUsrName & ".txt" , "\\MyDomain\files\IT\Support\Staff Change Requests\GroupMembershipQueries\"
Set objWshShell = Wscript.CreateObject("WScript.Shell")
'objWshShell.Run "%comspec% /c notepad.exe " & "\\MyDomain\files\public\temp" & strUsrName & ".txt",0,True
objWshShell.Run "%comspec% /c notepad.exe " & "\\MyDomain\files\IT\Support\Staff Change Requests\GroupMembershipQueries\" & strUsrName & ".txt",0,True
set objFso = Nothing
set WriteToFile = Nothing
set ObjUser = Nothing
set objWshShell = Nothing
Re: VB Script returns all group memberships for a user EXCEPT Exchange Dist groups
I am posting below the finished product which works fine with the exception of as we've discussed, not including the user's "primary group".
How would I add this to the final output? I need to include the "Primary Group membership" as well in the final output, so let me know how I would go about that if you can
Script thus far:
Option Explicit
On Error Resume Next
Dim ObjUser, objFso, WriteToFile, strDomain, member, strUsrName, objWshShell, strTime
Dim strCurrenttime, strDateStart, strDateMid, strDateEnd, strSecond, strFileName
Dim strNTName, objTrans, strUserDN, objGroup
Const FileRead = 1
Const FileWrite = 2
Const OverwriteExisting = True
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1
strDomain = "MyDomain"
strUsrName = inputbox ("User Name", "Display User Group Membership")
strNTName = strDomain & "\" & strUsrName
' Use the NameTranslate object to convert the NT user name to the
' Distinguished Name required for the LDAP provider.
Set objTrans = CreateObject("NameTranslate")
' Initialize NameTranslate by locating the Global Catalog.
objTrans.Init ADS_NAME_INITTYPE_GC, ""
' Use the Set method to specify the NT format of the object name.
' Trap the error if the user does not exist.
On Error Resume Next
objTrans.Set ADS_NAME_TYPE_NT4, strNTName
If (Err.Number <> 0) Then
Wscript.Echo "User " & strUsrName & " not found."
Wscript.Quit
End If
On Error GoTo 0
strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)
' Bind to the user object in Active Directory with the LDAP provider.
Set objUser = GetObject("LDAP://" & strUserDN)
' Setting relatively random file name so we don't overwrite due to multiple queries on same user
strCurrenttime = Now()
strDateStart = Left(DATE,1))
strDateEnd = Right(DATE,4)
strTime = Trim(strSecond)
set objFso = CreateObject("Scripting.FileSystemObject")
objFso.CreateTextFile(strUsrName & strDateStart & strDateEnd & strTime & ".txt")
set WriteToFile = objFso.OpenTextFile(strUsrName & strDateStart & strDateEnd & strTime & ".txt", FileWrite, True)
writetofile.write "These are the group memberships for " & strUsrName & vbcrlf
writetofile.write "Memberships as of " & strCurrenttime & vbcrlf & vbcrlf
writetofile.write "Groupname" & vbcrlf & vbcrlf
For Each objGroup In objUser.Groups
WriteToFile.write (Mid(objGroup.Name,4)) & vbcrlf
next
WriteToFile.close
objFSO.MoveFile strUsrName & strDateStart & strDateEnd & strTime & ".txt" , "\\MyDomain\files\IT\Support\Staff Change Requests\GroupMembershipQueries\"
Set objWshShell = Wscript.CreateObject("WScript.Shell")
objWshShell.Run "%comspec% /c notepad.exe " & "\\MyDomain\files\IT\Support\Staff Change Requests\GroupMembershipQueries\" & strUsrName & strDateStart & strDateEnd & strTime & ".txt",0,True
set objFso = Nothing
set WriteToFile = Nothing
set ObjUser = Nothing
set objWshShell = Nothing