Extract Group Members to a Text File

Author Message
esnmb

  • Total Posts : 453
  • Scores: 0
  • Reward points : 0
  • Joined: 1/11/2005
  • Location: USA
  • Status: offline
Extract Group Members to a Text File Wednesday, August 10, 2005 1:50 AM (permalink)
0
This script will search an OU for a specified Group Name based on Instr.  Create text files and adds the user to them.
 
  On Error Resume Next Set objShell = Wscript.CreateObject("Wscript.Shell")
objDesktop = objShell.SpecialFolders("Desktop") Set objFSO = CreateObject("Scripting.FileSystemObject") Set objOU = GetObject("LDAP://OU=IT,OU=Security Groups,DC=MyDomain,DC=com") For Each Group In objOU
 If InStr(Group.Name, "CN=TestGroupName") Then
  Set objFile = objFSO.CreateTextFile(objDesktop & "\" & Group.CN & ".txt", 2)
  objFile.WriteLine Group.CN
  objFile.WriteLine "======================="
  objFile.WriteLine
   
  Set objGroup = GetObject _
     ("LDAP://" & Group.Name & ",OU=IT,OU=Security Groups,DC=mlnusa,DC=com")
   objGroup.GetInfo
   
   arrMemberOf = objGroup.GetEx("member")

   For Each strMember In arrMemberOf
    Set objUser = GetObject("LDAP://" & strMember)
    objFile.WriteLine objUser.samaccountname  Next
  objFile.Close
 End If
Next MsgBox "Completed script.",64,"Informational"
 
#1
    rlawrason

    • Total Posts : 10
    • Scores: 2
    • Reward points : 0
    • Joined: 11/29/2006
    • Status: offline
    RE: Extract Group Members to a Text File Tuesday, December 12, 2006 2:41 AM (permalink)
    0
    I did a couple of things to your script to make it more user friendly.
    1) fixed line breaks
    2) fixed invalid calls to object Group.CN on lines 10, 11, & 16 (I assume you meant to call Group.Name and it was a typo)
    3) changed the output file on line 10 to "\Output_" (in my experience & statements using only "\" tend to fail)
    4) added some comments
    5) enclosed the code in a code box to preserve formatting

     On Error Resume Next 
     
     Set objShell = Wscript.CreateObject("Wscript.Shell")
     Set objFSO = CreateObject("Scripting.FileSystemObject") 
     Set objOU = GetObject("LDAP://OU=OU1,OU=OU2,DC=DOMAIN,DC=EXTENSION") 'input the full  LDAP  path to your group's  OU here 
     objDesktop = objShell.SpecialFolders("Desktop")
     
     For Each Group In objOU
     If InStr(Group.Name, "CN=NAMEOFGROUP") Then 'input the name of the AD group to query here
     Set objFile = objFSO.CreateTextFile(objDesktop & "\Output_" & Group.Name & ".txt", 2)
     objFile.WriteLine Group.Name
     objFile.WriteLine "======================="
     objFile.WriteLine
      
     Set objGroup = GetObject _
        ("LDAP://" & Group.Name & ",OU=OU1,OU=OU2,DC=DOMAIN,DC=EXTENSION")  '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)
       objFile.WriteLine objUser.samaccountname  
       Next
       objFile.Close
     End If 
     Next
     MsgBox "Completed script.",64,"Informational"
     
    <message edited by rlawrason on Tuesday, December 12, 2006 2:57 AM>
     
    #2
      A.Bedeker

      • Total Posts : 8
      • Scores: 0
      • Reward points : 0
      • Joined: 12/21/2006
      • Status: offline
      RE: Extract Group Members to a Text File Tuesday, January 02, 2007 8:12 PM (permalink)
      0
      I made some changes in the script to export only the members of a specified global group.
      Problem however is that after my changes I don't get txt files.. Could someone check my script?
       
      (I couldn't get it in a codebox...):
       
      On Error Resume Next
      gga= inputbox("Which global group?")
      Set objShell = Wscript.CreateObject("Wscript.Shell")
      Set objFSO = CreateObject("Scripting.FileSystemObject")
      Set objOU = GetObject("LDAP://OU=Application Management Group,OU=Security Groups,DC=AD,DC=Intra") 'input the full  LDAP  path to your group's  OU here
      objDesktop = objShell.SpecialFolders("Desktop")
      For Each Group In objOU
      If InStr(Group.Name, gga) Then 'input the name of the AD group to query here
      Set objFile = objFSO.CreateTextFile(objDesktop & "\Output_" & Group.Name & ".txt", 2)
      objFile.WriteLine Group.Name
      objFile.WriteLine "======================="
      objFile.WriteLine
       
      Set objGroup = GetObject _
        ("LDAP://" & Group.Name & ",OU=Application Management Group,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)
       objFile.WriteLine objUser.samaccountname 
       Next
       objFile.Close
      End If
      Next
      MsgBox "Completed script.",64,"Informational"
       
       
      #3
        Country73

        • Total Posts : 754
        • Scores: 10
        • Reward points : 0
        • Status: offline
        RE: Extract Group Members to a Text File Wednesday, January 03, 2007 2:53 AM (permalink)
        0
        (I left the 'code' blocks out to use colored text to show what I added)

        I added an echo so that you will get a message if the specified group is found. (bold red line)
        Do you get a response when you run this? If you don't, then that is why you don't get any text files.

        '===code===
        On Error Resume Next

        gga= inputbox("Which global group?")

        Set objShell = Wscript.CreateObject("Wscript.Shell")
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objOU = GetObject("LDAP://OU=Application Management Group,OU=Security Groups,DC=AD,DC=Intra") 'input the full  LDAP  path to your group's  OU here

        objDesktop = objShell.SpecialFolders("Desktop")

        For Each Group In objOU
            If InStr(Group.Name, gga) Then 'input the name of the AD group to query here
           
                wscript.echo gga & " group found"
           
                Set objFile = objFSO.CreateTextFile(objDesktop & "\Output_" & Group.Name & ".txt", 2)
                    objFile.WriteLine Group.Name
                    objFile.WriteLine "======================="
                    objFile.WriteLine

                    Set objGroup = GetObject("LDAP://" & Group.Name & ",OU=Application Management Group,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)
                            objFile.WriteLine objUser.samaccountname
                    Next
                    objFile.Close
            End If
        Next
        MsgBox "Completed script.",64,"Informational"

        '===end code===
         
        #4
          A.Bedeker

          • Total Posts : 8
          • Scores: 0
          • Reward points : 0
          • Joined: 12/21/2006
          • Status: offline
          RE: Extract Group Members to a Text File Wednesday, January 03, 2007 8:15 PM (permalink)
          0
          The script worked just fine... the globalgroup name was in capital....I had to enter the exact name.
           
          #5
            A.Bedeker

            • Total Posts : 8
            • Scores: 0
            • Reward points : 0
            • Joined: 12/21/2006
            • Status: offline
            RE: Extract Group Members to a Text File Wednesday, January 03, 2007 10:12 PM (permalink)
            0
            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
             
            #6

              Online Bookmarks Sharing: Share/Bookmark

              Jump to:

              Current active users

              There are 0 members and 1 guests.

              Icon Legend and Permission

              • New Messages
              • No New Messages
              • Hot Topic w/ New Messages
              • Hot Topic w/o New Messages
              • Locked w/ New Messages
              • Locked w/o New Messages
              • Read Message
              • Post New Thread
              • Reply to message
              • Post New Poll
              • Submit Vote
              • Post reward post
              • Delete my own posts
              • Delete my own threads
              • Rate post

              2000-2012 ASPPlayground.NET Forum Version 3.9