Solved

VB Script Needed to query user information from AD

Posted on 2011-03-15
1
436 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 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