Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Extract Group Members to a Text File

 
Logged in as: Guest
arrSession:exec spGetSession 2,16,24661
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> Post a VBScript >> Extract Group Members to a Text File
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Extract Group Members to a Text File - 8/10/2005 12:50:26 AM   
  esnmb

 

Posts: 441
Score: 0
Joined: 1/11/2005
From: USA
Status: offline
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"
 
 
Post #: 1
 
 RE: Extract Group Members to a Text File - 12/12/2006 1:41:51 AM   
  rlawrason

 

Posts: 9
Score: 0
Joined: 11/29/2006
Status: offline
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


      

< Message edited by rlawrason -- 12/12/2006 1:57:20 AM >

(in reply to esnmb)
 
 
Revisions: 2 | Post #: 2
 
 RE: Extract Group Members to a Text File - 1/2/2007 7:12:53 PM   
  A.Bedeker


Posts: 8
Score: 0
Joined: 12/21/2006
Status: offline
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"

(in reply to rlawrason)
 
 
Post #: 3
 
 RE: Extract Group Members to a Text File - 1/3/2007 1:53:18 AM   
  Country73


Posts: 710
Score: 8
Joined: 8/25/2004
From: USA
Status: offline
(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===

(in reply to A.Bedeker)
 
 
Post #: 4
 
 RE: Extract Group Members to a Text File - 1/3/2007 7:15:22 PM   
  A.Bedeker


Posts: 8
Score: 0
Joined: 12/21/2006
Status: offline
The script worked just fine... the globalgroup name was in capital....I had to enter the exact name.

(in reply to Country73)
 
 
Post #: 5
 
 RE: Extract Group Members to a Text File - 1/3/2007 9:12:29 PM   
  A.Bedeker


Posts: 8
Score: 0
Joined: 12/21/2006
Status: offline
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

(in reply to A.Bedeker)
 
 
Post #: 6
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> Post a VBScript >> Extract Group Members to a Text File Page: [1]
Jump to:





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
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts