[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

VB Script Needed to extract user information from active directory

Posted on 2011-03-09
3
Medium Priority
?
413 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 2000 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

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

650 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