1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Query Active Directory using Employee ID

Discussion in 'Visual Basic ( VB )' started by RTorres9, Jul 22, 2008.

  1. RTorres9

    RTorres9 New Member

    Jul 21, 2008
    Likes Received:
    Trophy Points:
    Hello Guy's

    I need help on developing a query script to search for specific employee ID. Currently I'm having to disable about 50 user accounts per day and need a script to help me program this. I've developed some code already that read's a value from a file and then it queries AD but is not finding anything when I search for the value of the "Employee ID" value.

    When I change to code to query for a specific user's job title it does fine accounts.

    Here is the code that I have put together already but it's coming up empty when I look for specific employee id's.
    Dim sCSVFile, objFSO, objTextFile
    Dim qQuery, objConnection, objCommand, objRecordSet
    Dim objUser
    Dim sempid	'usage to capture the user's employee ID
    Dim suserid	'usage to capture the user's sAMAccountname
    Dim supn	'usage to capture the user's usersprincipalname
    Dim suname 	'usage to capture the user's full name
    Dim susn		'usage to capture the user's surname
    Dim sugn	'usage to capture the user's givenname
    Const ForReading = 1
    sCSVFile = "C:\repository\test\refemployid.csv"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile(sCSVFile,ForReading)
    Do Until objTextFile.AtEndOfStream
    	TxtIn = objTextFile.ReadLine
    	qQuery = "<LDAP://dc=yisd,dc=net>;" & _
    		"(&(objectCategory=user)(employee ID=TxtIn));" &_
    	Set objConnection = CreateObject("ADODB.Connection")
    	Set objCommand = CreateObject("ADODB.Command")
    	Set objRecordSet = CreateObject("ADODB.Recordset")
    	objConnection.Open "Provider=ADsDSOObject;"
    	objCommand.ActiveConnection = objConnection
    	objCommand.Properties("page size")= 100
    	objCommand.CommandText = qQuery
    	Set objRecordSet = objCommand.Execute
    	While Not objRecordSet.EOF
    	Set objUser = GetObject(objRecordSet("adspath"))
    		RDData 					'Read attributes into variables
    		'WDData 					'Write data to file
    		'WScript.Echo objuser.employeeID
    		'WScript.Echo objuser.userPrincipalName
    		'WScript.Echo objuser.name
    		'WScript.Echo objuser.sn
    	Sub RDData
    		sempid = objuser.employeeID
    		suserid	= objuser.sAMAccountName
    		supn = objuser.userPrincipalName
    		suname = objuser.name
    		susn = objuser.sn 
     		sugn = objuser.givenName
    	End Sub
    	Sub VWData
    		WScript.Echo sempid
    		WScript.Echo sserid
    		WScript.Echo supn
    		WScript.Echo suname
    		WScript.Echo suserid
    		WScript.Echo sugn
    	End Sub
    WScript.Echo "Finished - All Done"
    Any assistance would be greatly appreciated, Thanks.
    Last edited by a moderator: Jul 23, 2008
  2. RogerAndes

    RogerAndes New Member

    Oct 3, 2008
    Likes Received:
    Trophy Points:
    Hey man lose the space between employee and ID in your ldap command. that should get you goin
  3. ssampati

    ssampati New Member

    Nov 10, 2008
    Likes Received:
    Trophy Points:
    Guys: i need some more help in using this script. What is the format of csv file? can i replace employeeid with employeenumber?

    In my org ...we have employeenumber attribute for employee id;s. i have a requirement to read the employee id's from a text/csv file and disable those id's and move them to a new ou called "disabled accts"

    Any help greatly appreciated.


Share This Page