How do I use a script to pass credentials to connect to Active Directory (AD)?
John Savill
October 25, 2006
1 Min Read
A. Normally, when you bind to AD, it uses your current credentials to determine the access level you have. You can, however, pass credentials by opening a Directory Services object instead and passing a username and password string. The following script, which you can download hereasks for a username and password, then uses them to connect to AD. It then reads an object from AD.
On Error Resume NextConst ADS_SECURE_AUTHENTICATION = 1Const ADS_USE_ENCRYPTION = 2Dim uid, pwd, ldapPathldapPath = "LDAP://CN=Clark Kent,OU=Justice League,DC=savilltech,dc=com"WScript.StdOut.Write "User name (with domain prefix): " uid = WScript.StdIn.ReadLineWScript.StdOut.Write "Please enter your password:" pwd = WScript.StdIn.ReadLineDim LDAP 'As IADsOpenDSObject Set LDAP = GetObject("LDAP:") Set obj = LDAP.OpenDSObject(ldapPath, uid, pwd, _ADS_USE_ENCRYPTION OR ADS_SECURE_AUTHENTICATION)if err.number0 then wscript.echo "Error connecting to AD " & err.number, err.description err.Clear Wscript.Quit(0)end ifwscript.echo "SAM Account name is " & obj.sAMAccountName
The important line is the LDAP.OpenDSObject, which uses the username and password entered, as the following code shows:
D:projectsVBScripts>cscript authtoad.vbsUser name (with domain prefix): savilltechbrucePlease enter your password:passwordSAM Account name is clark
You don't have to prompt for username or password; you can just set them as string variables or even hard code them, as the following example shows:
uid = "savilltechbruce"pwd = "password"Dim LDAP 'As IADsOpenDSObject Set LDAP = GetObject("LDAP:") Set obj = LDAP.OpenDSObject(ldapPath, uid, pwd, _ ADS_USE_ENCRYPTION OR ADS_SECURE_AUTHENTICATION)
About the Author
Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.
You May Also Like