?
Solved

Active Directory Query/Export

Posted on 2006-10-26
3
Medium Priority
?
183 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 500 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

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…
I've always wanted to allow a user to have a printer no matter where they login. The steps below will show you how to achieve just that. In this Article I'll show how to deploy printers automatically with group policy and then using security fil…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

777 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