Solved

VB Script Needed to extract user information from active directory

Posted on 2011-03-09
3
409 Views
Last Modified: 2012-05-11
I am needing to gain a list of users where their Department equals something specific.  For example if Department = Merchandising I need a csv file that contains the users:

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 department name to query.
0
Comment
Question by:seaninman
[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
  • 2
3 Comments
 
LVL 12

Expert Comment

by:prashanthd
ID: 35083103
Try the following code....


On Error Resume Next

dpname="" 'mention departname to check
outputfile="C:\output.csv"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(outputfile, 2)


Dim objRootDSE, strDomain, strUsername, objConnection, objCommand, objRecordSet, strDN

' Get domain components
Set objRootDSE = GetObject("LDAP://RootDSE")
strDomain = objRootDSE.Get("DefaultNamingContext")

' Set ADO connection
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

' Set ADO command
Set objCommand = CreateObject("ADODB.Command")
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

objCommand.CommandText = "SELECT givenName,sn,mail,samaccountname FROM 'LDAP://" & strDomain & "' WHERE objectCategory='person' and department='"& dpname &"'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False
' Set recordset to hold the query result
Set objRecordSet = objCommand.Execute

objTextFile.WriteLine "First Name,Last Name, AccountName, Mail"

Do While Not objRecordSet.EOF
   
    strfName = objGroup.Get("givenName")
    strlName = objGroup.Get("sn")
    strmail = objGroup.Get("mail")
    strSAMAccountName = objGroup.Get("sAMAccountName")
   
    objTextFile.WriteLine strfName &","& strlName &","& strSAMAccountName &","& strmail
   
    objRecordSet.MoveNext
Loop

objTextFile.Close

WScript.Echo "Done, Check output file"
0
 
LVL 8

Accepted Solution

by:
jawa29 earned 500 total points
ID: 35083127
Hi seaninman

This should do what your asking.

Jawa29


Const ForWriting = 2

Set oRoot = GetObject("LDAP://RootDSE")
vDomain = oRoot.Get("DefaultNamingContext") 

vDepartment = InputBox("Department","Department","Enter Department")

Set oConnection = CreateObject("ADODB.Connection")
Set oCommand =   CreateObject("ADODB.Command")
oConnection.Provider = "ADsDSOObject"
oConnection.Open "Active Directory Provider"
Set oCommand.ActiveConnection = oConnection
oCommand.Properties("Page Size") = 1000

oCommand.CommandText = "<LDAP://" & vDomain & ">;(&(!userAccountControl:1.2.840.113556.1.4.803:=65536)((objectCategory=Person)(department=" & vDepartment & ")));" &_
	"givenName, sn, mail, samAccountName,;Subtree"
Set oRS = oCommand.Execute

sTxt = "Firstname,Surname,Logon,Email" & vbCrLf
Do While Not oRS.EOF
	sTxt = sTxt & oRS("givenName") & "," & oRS("sn") & "," & oRS("mail") & "," & oRS("samAccountName") & vbCrLf
	oRS.MoveNext
Loop

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oTextFile = oFSO.OpenTextFile("c:\UserExport.txt", ForWriting, True)
oTextFile.WriteLine(sTxt)
oTextFile.Close

Open in new window

0
 
LVL 12

Expert Comment

by:prashanthd
ID: 35083295
hmmm...some errors in my earlier code, you can try this
'On Error Resume Next

dpname="IEMC" 'mention departname to check
outputfile="C:\output.csv"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.createTextFile(outputfile, 2)

Const ADS_SCOPE_SUBTREE = 2

Dim objRootDSE, strDomain, strUsername, objConnection, objCommand, objRecordSet, strDN

' Get domain components
Set objRootDSE = GetObject("LDAP://RootDSE")
strDomain = objRootDSE.Get("DefaultNamingContext")

' Set ADO connection
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

' Set ADO command
Set objCommand = CreateObject("ADODB.Command")
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

objCommand.CommandText = "SELECT givenName,sn,mail,samaccountname FROM 'LDAP://" & strDomain & "' WHERE 

objectCategory='person' and department='"& dpname &"'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False
' Set recordset to hold the query result
Set objRecordSet = objCommand.Execute

objTextFile.WriteLine "First Name,Last Name, AccountName, Mail"

Do While Not objRecordSet.EOF 
    
    strfName = objRecordSet.Fields("givenName").Value
    strlName = objRecordSet.Fields("sn").Value
    strmail = objRecordSet.Fields("mail").Value
    strSAMAccountName = objRecordSet.Fields("sAMAccountName").Value
    
    objTextFile.WriteLine strfName &","& strlName &","& strSAMAccountName &","& strmail
wscript.echo strfName &","& strlName &","& strSAMAccountName &","& strmail
    
    objRecordSet.MoveNext
Loop

objTextFile.Close

WScript.Echo "Done, Check output file"

Open in new window

0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

717 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