Results 1 to 7 of 7

Thread: VB Script returns all group memberships for a user EXCEPT Exchange Dist groups

  1. #1
    Join Date
    Mar 2009
    Posts
    10

    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

  2. #2
    Join Date
    Oct 2005
    Posts
    725

    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?

  3. #3
    Join Date
    Jan 2006
    Posts
    830

    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.

  4. #4
    Join Date
    Mar 2009
    Posts
    10

    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

  5. #5
    Join Date
    Aug 2006
    Posts
    201
    I also would like to add further, you can also find a sample script at this link - http://technet.microsoft.com/en-us/m...iptingguy.aspx. Note that, this one does not take into account the primary group membership.

  6. #6
    Join Date
    Mar 2009
    Posts
    10

    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

  7. #7
    Join Date
    Feb 2011
    Posts
    1
    I am very new to vb scripting.

    Can you please tell me how to get output with this script with example.

Similar Threads

  1. Replies: 6
    Last Post: 19-10-2010, 04:54 AM
  2. VB script - List all user groups Help
    By Daljeet in forum Software Development
    Replies: 4
    Last Post: 18-12-2008, 07:51 PM
  3. Script to create multiple groups
    By Saphire in forum Active Directory
    Replies: 1
    Last Post: 18-08-2008, 11:51 PM
  4. Export All Active Directory Group memberships to a .txt file
    By RandomEcho in forum Windows Server Help
    Replies: 1
    Last Post: 13-07-2007, 05:31 AM
  5. Need CSVDE to dump user group memberships
    By Spin in forum Windows Server Help
    Replies: 4
    Last Post: 18-01-2006, 08:26 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,616,210.58883 seconds with 17 queries