Solved

Active Directory Query/Export

Posted on 2006-10-26
3
178 Views
Last Modified: 2010-08-05
I want to export the  list of members of a Distribution List to a plain text file. Does anyone know how to do this?
0
Comment
Question by:debbie0040
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 71

Accepted Solution

by:
Chris Dent earned 125 total points
ID: 17813170

Hi,

I have a script for doing that, it's quite long and doesn't need to be anywhere near as long for what you actually want to do. But it's that way because it's flexible and because I don't have to write new bits of code for it. Just feed is the group name on the command line. e.g.:

cscript FindGroup.vbs "Sales Users" -f SalesUsers.txt

There are a few other options depending on what you'd like to do with the information. Otherwise, here you go (save as .vbs):


' A script that looks for a Group
'
' Author: Chris Dent
' Date: 10/02/2006

Option Explicit

' Global Constants

Const ADS_SCOPE_SUBTREE = 2

' Global Variables

Dim objFileSystem, objFile
Dim strSearchPattern, strDomainName, strFileName
Dim booListSAMName, booListDN, booWriteToFile, booExactOnly, booCount

'
' Subroutines
'

Sub UsageText

      Dim strMessage

      strMessage = "Usage:" & VbCrLf & VbCrLf
      strMessage = strMessage & "cscript " & WScript.ScriptName & " <Search String> [-s] [-dn] [-c]" & VbCrLf
      strMessage = strMessage & VbTab & VbTab & "[-d <Domain Name>] [-f <File Name>]" & VbCrLf
      strMessage = strMessage & VbCrLf
      strMessage = strMessage & VbTab & "-e - Exact Match Only" & VbCrLf
      strMessage = strMessage & VbTab & "-c - Count Members" & VbCrLf
      strMessage = strMessage & VbTab & "-s - Display sAMAccountName rather than Users Name" & VbCrLf
      strMessage = strMessage & VbTab & "-dn - Display distinguishedName rather than Users Name" & VbCrLf
      strMessage = strMessage & VbTab & "-d <Domain Name> - Search an alternate AD Domain" & VbCrLf
      strMessage = strMessage & VbTab & "-f <File Name> - Writes all Output to Specified File" & VbCrLf
      WScript.Echo strMessage
      WScript.Quit
End Sub

Sub SortArgv

      ' Checks the Command line parameters and updates the appropriate fields.

      Dim objArgv, objRootDSE
      Dim strArgv
      Dim booSearchAltDomain
      Dim i, intDomain, intFile

      Set objArgv = WScript.Arguments

      If objArgv.Count < 1 Then
            UsageText()
      End If

      booExactOnly = False
      booCount = False
      booListSAMName = False
      booListDN = False
      booSearchAltDomain = False
      booWriteToFile = False

      strSearchPattern = objArgv(0)

      i = 0

      For Each strArgv in objArgv
            i = i + 1
            If LCase(strArgv) = "-e" Then
                  booExactOnly = True
            End If
            
            If LCase(strArgv) = "-c" Then
                  booCount = True
            End If
            
            If LCase(strArgv) = "-s" Then
                  booListSAMName = True
            End If

            If LCase(strArgv) = "-dn" Then
                  booListDN = True
            End If

            If LCase(strArgv) = "-d" Then
                  booSearchAltDomain = True
                  intDomain = i
            End If

            If LCase(strArgv) = "-f" Then
                  booWriteToFile = True
                  intFile = i
            End If
      Next

      If (objArgv.Count > 1) And (booListSAMName = False) And _
            (booListDN = False) And (booWriteToFile = False) And _
            (booExactOnly = False) And (booCount = False) And _
            (booSearchAltDomain = False) Then
                  UsageText()
      End If

      If booSearchAltDomain = True Then
            If objArgv.Count < (intDomain +  1) Then
                  UsageText()
            Else
                  strDomainName = "DC=" & Replace(objArgv(intDomain), ".", ",DC=")
                  PrefixDC strDomainName
            End If
      Else
            Set objRootDSE = GetObject("LDAP://RootDSE")
            strDomainName = objRootDSE.Get("defaultNamingContext")
            Set objRootDSE = Nothing
      End If

      If booWriteToFile = True Then
            If objArgv.Count < (intFile + 1) Then
                  UsageText()
            Else
                  strFileName = objArgv(intFile)
            End If
      End If

      Set objArgv = Nothing      
End Sub


Sub FindGroup(strSearchPattern)

      Dim objConnection, objCommand, objRecordSet, objMember, objGroup
      Dim strAddress, strLine, strGroupName
      Dim arrAddresses
      Dim booFound
      Dim i

      Set objConnection = CreateObject("ADODB.Connection")
      objConnection.Provider = "ADsDSOObject"
      objConnection.Open "Active Directory Provider"

      Set objCommand = CreateObject("ADODB.Command")
      objCommand.ActiveConnection = objConnection

      objCommand.CommandText = "SELECT name, aDSPath " &_
            "FROM 'LDAP://" & strDomainName & "' WHERE objectClass='group'"

      objCommand.Properties("Page Size") = 1000
      objCommand.Properties("Timeout") = 600
      objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
      objCommand.Properties("Cache Results") = False

      Set objRecordSet = objCommand.Execute

      While Not objRecordSet.EOF
            booFound = False
            i = 0

            On Error Resume Next
            strGroupName = objRecordSet.Fields("name")
            If booExactOnly = True Then
                  If LCase(strGroupName) = LCase(strSearchPattern) Then
                        booFound = True
                  End If
            Else
                  If InStr(1, strGroupName, strSearchPattern, VbTextCompare) Then
                        booFound = True
                  End If
            End If

            If booFound = True Then
                  strLine = "Listing Members for " & strGroupName & VbCrLf

                  Set objGroup = GetObject(objRecordSet.Fields("aDSPath"))
                  For Each objMember in objGroup.Members
                        i = i + 1
                        If booListSAMName = True Then
                              strLine = strLine & objMember.Get("sAMAccountName") & VbCrLf
                        ElseIf booListDN = True Then
                              strLine = strLine & objMember.Get("distinguishedName") & VbCrLf
                        Else
                              strLine = strLine & objMember.Get("displayName") & VbCrLf
                        End If
                  Next

                  If booCount = True Then
                        strLine = strLine & "Member Count: " & i & VbCrLf
                  End If

                  If booWriteToFile = True Then
                        objFile.WriteLine strLine
                  Else
                        WScript.Echo strLine
                  End If
            End If
            On Error Goto 0
            objRecordSet.MoveNext
      Wend

      objConnection.Close

      Set objRecordSet = Nothing
      Set objCommand = Nothing
      Set objConnection = Nothing
End Sub


'
' Main Code
'

Set objFileSystem = CreateObject("Scripting.FileSystemObject")

SortArgv

If booWriteToFile = True Then
      Set objFile = objFileSystem.OpenTextFile(strFileName, 2, True, 0)
End If

FindGroup strSearchPattern

Set objFile = Nothing
Set objFileSystem = Nothing
0

Featured Post

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to customise Office 2016 font settings with a GPO 3 106
Connecting two servers 30 93
inplace upgrade from Windows 2003 R2 to 2012 8 152
Auto Login Script 3 82
On July 14th 2015, Windows Server 2003 will become End of Support, leaving hundreds of thousands of servers around the world that still run this 12 year old operating system vulnerable and potentially out of compliance in many organisations around t…
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question