Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script seeing input variable value as blank but sees hard-coded value 4

Status
Not open for further replies.

bdwilcox

Technical User
Jul 7, 2022
13
US
With this script, if I use the input variable in the line sAMAccountName=UserLogonName, the code doesn't work. But if I hard-code the value of sAMAccountName with something like sAMAccountName=bdwilcox instead, the code works fine. I've tried adding single and double quotes to the value of UserLogonName and it still doesn't work. Any insight as to what I'm doing wrong would be appreciated.

Here's the full script (with the line that doesn't work highlighted):

DIM UserLogonName, TrimName, result

UserLogonName = InputBox ("==== Find Parent OU(s) of AD User Account ====" & vbCrLf & vbCrLf & "Enter the username of the person whose AD account's parent OU(s) you want to check, then press the OK button:","Find Parent OU(s) of AD User Account v.1")

UserLogonName=trim(UserLogonName)

If UserLogonName = "" then Wscript.Quit

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

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

objCommand.CommandText = _
"<GC://ou=User accounts,dc=domain,dc=com>;" & _
"(&(objectCategory=person)(objectClass=user)" & _
"(sAMAccountName=UserLogonName));" & _
"sAMAccountName, distinguishedName;subtree"

Set objRecordSet = objCommand.Execute

If objRecordSet.RecordCount = 0 Then
Wscript.Echo "The sAMAccountName is not in use."
Else
While Not objRecordset.EOF
Wscript.Echo "sAMAccountName = " & _
objRecordset.Fields("sAMAccountName")
Wscript.Echo "distinguishedName = " & _
objRecordset.Fields("distinguishedName")
objRecordset.MoveNext
Wend
End If

objConnection.Close
 
In:
"(sAMAccountName=UserLogonName));" & _
you have UserLogonName in string, I guess you need a variable here:
"(sAMAccountName=" & UserLogonName & "));" & _
 
Code:
' Concatenate the UserLogonName into the query

objCommand.CommandText = _
"<GC://ou=User accounts,dc=domain,dc=com>;" & _
"(&(objectCategory=person)(objectClass=user)" & _
"(sAMAccountName=" & UserLogonName & "));" & _
"sAMAccountName, distinguishedName;subtree"
 
To add a little value on the conversation, I recommend you use more variables to make the code clearer. Additionally, you are shooting yourself in the foot by hard-coding the domain in the script, when you could just query for it.

Code:
' Get Root AD object via query rather than hard coded value.
Set oRootDSE = GetObject("LDAP://RootDSE")
SearchRoot = "<LDAP://" & oRootDSE.Get("defaultNamingContext") & ">"
Filter = "(&(objectCategory=person)(objectClass=User)(sAMAccountName=" & UserLogonName & "))"
Attribs = "sAMAccountName,distinguishedName"
Scope = "subtree"
objCommand.CommandText = SearchRoot & ";" & Filter & ";" & Attribs & ";" & Scope

HTH
 
Thanks, everyone for the info and feedback. Let me try out the suggestions here and I will let you know how it goes.

-bw
 
Thanks, everyone, for the input. I should have laid the line out as one line and deconstructed it because the way it was broken up it was confusing me. Appreciate all the help! And thanks for the excellent add, PScottC. I will incorporate those ideas into future scripts.

Appreciate all your help, insight and time!

-bdwilcox
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top