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):
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