Solved

VB Script Needed to query user information from AD

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Automate switch config backup 3 70
Extract field headers and values between quotes 12 53
VBScript on Html 15 47
randomize data from excell cell 15 45
Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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