Solved

Active Directory Query/Export

Posted on 2006-10-26
3
173 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
3 Comments
 
LVL 70

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Scenerio: You have a server running Server 2003 and have applied a retail pack of Terminal Server Licenses.  You want to change servers or your server has crashed and you need to reapply the Terminal Server Licenses. When you enter the 16-digit lic…
A quick step-by-step overview of installing and configuring Carbonite Server Backup.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now