Question Locked

VBS Script and using variables

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hello everyone,

I don't have much scripting experience, but I've managed to build one for searching Active Directory for a given username. What I'm looking to do is have the user prompted for his/her username and have that variable used for the search. I've gotten that worked out, but what i can't figure out is using the variable in the following code

'Find Full Name from login name
objCommand.CommandText = _
"SELECT Name FROM 'LDAP://dc=xxx,dc=xxx,dc=com' WHERE objectCategory='user' " & _
"AND sAMAccountName='gptest'"
Set objRecordSet = objCommand.Execute

WScript.Echo "Full Name is " & objRecordset.Fields("Name").value

Where it says "AND sAMAccountName='gptest'", if I replace gptest with the variable strUserName the code errors out.
I've tried sAMAccountName=strusername SamAccountName='strusername'
SamAccountName= & strusername
and a few others.

I'll put the whole code up for reference. It will be trimmed down some when its ready to be deployed, right now its just in the 'new coder play with everything' phase. Any suggestions on how to insert the variable in there are welcome!

dim strusername
strUserName = "gptest" 'once the rest of the 'script works this will be replaced with the 'prompt code
dtStart = TimeValue(Now())
Const ADS_SCOPE_SUBTREE = 2

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

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

objCommand.CommandText = _
";(&(objectCategory=User)" & _
"(samAccountName=" & strUserName & "));samAccountName;subtree"

Set objRecordSet = objCommand.Execute

If objRecordset.RecordCount = 0 Then
WScript.Echo "sAMAccountName: " & strUserName & " does not exist."

Else
WScript.Echo strUserName & " exists."

'Find Full Name from login name
objCommand.CommandText = _
"SELECT Name FROM 'LDAP://dc=xxx,dc=xxx,dc=com' WHERE objectCategory='user' " & _
"AND sAMAccountName='gptest'"
Set objRecordSet = objCommand.Execute

WScript.Echo "Full Name is " & objRecordset.Fields("Name").value

'Find login Script for user
objCommand.CommandText = _
"SELECT ScriptPath FROM 'LDAP://dc=xxx,dc=xxx,dc=com' WHERE objectCategory='user' " & _
"AND sAMAccountName='gptest'"
Set objRecordSet = objCommand.Execute
Wscript.Echo "Script Path: " & objRecordset.Fields("ScriptPath").value

' Wscript.Echo "Home Directory: " & objUser.HomeDirectory
' Wscript.Echo "Home Drive: " & objUser.HomeDrive
End If

EDIT: Apologies for the format of the code, Forum box altered it slightly.

5 replies

by faradhi

AND sAMAccountName='" & strUserName & "'"

by matt.johnstone

Actually no I didn't. Now that I see it, it seems so obvious. Thanks a lot.

I also added the username prompt to the code and it works wonderfully.

Another question for you all, is there a way I can use that method to ask the user to input their password, and check it against the AD password?

by ic-it

their logging on to the Domain equate to a positive check?

by matt.johnstone

Yes it does. Unfortunately my higher ups want to limit the account that can log into our laptops to one account. He then wants them to start Cisco VPN software to connect to our network, and then run a script to connect their drives with permissions ie: netuse blah blah user password. Where they are prompted to supply the username, and password and then they both are checked against AD. Its the way they want it, so I've been trying to get it near to that as possible.

I think I'm going to try and get away with a simple: ask user name and password and dump the vars at the end of a net use command. I don't know if They'll approve that without some kind of AD check. Thats why I was asking if it might be possible.

This discussion is closed to new replies.