• 0

[VBScript] LDAP query to pull DN


Question

Writing a script that basically does this:

1. Ask user for a username

2. Ask user for employee number

3. script verifies if it is real username

- if it is, continues

- if not, it stops script

4. (step i need help with) - Query AD through LDAP and pull DN

5. Add employee number to 2 different fields in AD (employeeID, employeeNumber)

Real simple script.. but having some issues getting the DN from AD.

*DN = distinguishedName

Here is what i got:

Set objFile = CreateObject("Scripting.FileSystemObject")
Set objNetName = CreateObject("WScript.NetWork") 

DIM strEmpID
Name = GetUserName()
EmployeeID = GetEmployeeID()
UserExists = CheckUser(Name)

If UserExists = TRUE then
msgbox strEmpID
msgbox Name
Call LoadEmployeeID
Else
Msgbox "User does not exist, try again."
wscript.quit(0)
End If

'===========================
Function LoadEmployeeID()
Set objUser = GetObject

objUser.Put "employeeNumber", strEmpID
'objUser.Put "employeeID", EmployeeID
objUser.SetInfo

End Function
'===========================
Function GetEmployeeID()
EmployeeID = Inputbox ("Please enter in 5 digit employee number")
strEmpID = EmployeeID
End Function

'=======================================
Function GetUserName()

i = 0
UserName = InputBox ("Ex. Firstname.LastName ", " Please Enter User Name")

Do While Instr(UserName, ".") = 0 and i <= 5

Username = InputBox ("Ex. Firstname.LastName ", " Please Enter User Name")

i=i+1

Loop

If Instr(UserName, ".") = 0 Then
WScript.Quit(0)
Else

GetUserName = Trim(UserName)
End If
End Function
'===========================================
Function CheckUser(strUserName)

dtStart = TimeValue(Now())
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"

Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection

objCommand.CommandText = _
	"<LDAP:/*removed*t>;(&(objectCategory=User)" & _
		 "(samAccountName=" & strUserName & "));samAccountName;distinguishedName;subtree"

Set objRecordSet = objCommand.Execute

If objRecordset.RecordCount = 0 Then
	'WScript.Echo "sAMAccountName: " & strUserName & " does not exist."
	CheckUser = FALSE
Else
	objRecordset.MoveFirst
	Do while Not objRecordset.EOF
 	wscript.echo objRecordset("samAccountName") & " | " & objRecordset("distinguishedName")
	objRecordset.MoveNext
	Loop
	'WScript.Echo strUserName & " exists."
	CheckUser = TRUE
End If

objConnection.Close

End Function
'====================================================

Can anyone please give some advice or code that can help?

I think if i can just pull the DN from LDAP query and put that into a variable..i can finish the rest.

Link to comment
https://www.neowin.net/forum/topic/708752-vbscript-ldap-query-to-pull-dn/
Share on other sites

4 answers to this question

Recommended Posts

  • 0

will this do:

Function GetDN(username)
	Set conn = CreateObject("ADODB.Connection")
	conn.Open "Provider=ADsDSOObject;"

	Set command = CreateObject("ADODB.Command")
	command.ActiveConnection = conn

	command.CommandText = "<LDAP:*removed*>;(&(objectClass=user)(sAMAccountName=" & username & "));distinguishedName;subtree"

	Set record = command.Execute

	If (record.RecordCount = 0) Then
		GetDN = Nothing 'or "" if you wanted...
	Else
		GetDN = record("distinguishedName")
	End If

	conn.Close
End Function

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.