Solved

VB Script Needed to query user information from AD

Posted on 2011-03-15
1
431 Views
Last Modified: 2012-05-11
I am needing to gain a list of users in a specific security or distribution group.  For example the query needs to provide the following information in a csv file.

first name,last name,username,email address

It would be nice if when the script is executed it would pop up a window asking for the specific group name to query.
0
Comment
Question by:seaninman
1 Comment
 
LVL 28

Accepted Solution

by:
omgang earned 500 total points
ID: 35138050
You'll need to change the domain value.
OM Gang


'Script begins here
'writes the names and e-mail addresses of the members of the domain group entered by user
'to a csv file on the users desktop

Dim objGroup, objUser, objFSO, objFile, objShell, objObject
Dim strDomain, strGroup, Domain, Group, strUserName, strEmail, strLName, strFName, strUserProfilePath, strPhone

      'local domain
strDomain = "MyDomain"

      'prompt for the name of the group whose members we want to export
strGroup = InputBox ("Enter AD group you want to export", "Group Name?")

      'create file system and shell objects
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")

      'get path to logged on users profile so we can write to their desktop directory
strUserProfilePath = objShell.ExpandEnvironmentStrings("%UserProfile%")

      'create destination text file in users desktop directory
      'name it the same as the group being queried
Set objFile = objFSO.CreateTextFile(strUserProfilePath & "\Desktop\" & strGroup & ".csv")

      'get the specified group in the specified domain
Set objGroup = GetObject("WinNT://" & strDomain & "/" & strGroup & ",group")


      'iterate through the user objects in the group
      'ignore non-user objects such as nested groups
For Each objUser In objGroup.Members
      strUserName = objUser.Name
            'set ADODB connection to LDAP so we can query for this user object and retrieve field values
      Set RootDSE = GetObject("LDAP://RootDSE")
      searchRoot = RootDSE.Get("defaultNamingContext")

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

      strQueryText = "<LDAP://" & searchRoot & ">;(&(objectCategory=Person)(samAccountName=" & strUserName &"));" _
            & "givenName,sn,mail,telephonenumber,ADsPath;SubTree"
      objCommand.CommandText = strQueryText
      objCommand.Properties("Page Size") = 2000
      objCommand.Properties("Timeout") = 60
      objCommand.Properties("Cache Results") = False
      Set objRecordSet = objCommand.Execute
      objRecordSet.MoveFirst

      ' --- Loop through the returned records
      Do Until objRecordSet.EOF
            strFName = objRecordSet.Fields("givenName").value
            strLName = objRecordSet.Fields("sn").value
            strEmail = objRecordSet.Fields("mail").value
            strPhone = objRecordSet.Fields("telephonenumber").value
            objRecordSet.MoveNext
      Loop

            'write field values to file
      objFile.WriteLine strUserName & "," & strLName & "," & strFName & "," & strEmail & "," & strPhone


Next
objFile.Close
Set objFile = Nothing
Set objFSO = Nothing
Set objUser = Nothing
Set objGroup = Nothing
Set objShell = Nothing
Wscript.Echo "Done.  Please check your Desktop for the output file"
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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
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…

708 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

13 Experts available now in Live!

Get 1:1 Help Now