?
Solved

VB Script Needed to extract user information from active directory

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

770 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