Using LDAP to retrieve the user name and password and start logon script

<< Click to Display Table of Contents >>

Navigation:  Macro Samples >

Using LDAP to retrieve the user name and password and start logon script

Set QuickExit = False
Set jRoot = GetObject("LDAP://RootDSE")
DomainPath = jRoot.Get("DefaultNamingContext")
Set Domain = GetObject("LDAP://" + DomainPath)
DomainPath = "LDAP://s1-mes.intra.company.com/DC=intra,DC=company,DC=com"
Set con = CreateObject("ADODB.Connection")
Set Com = CreateObject("ADODB.Command")
con.Provider = "ADsDSOObject"
con.Open "Active Directory Provider"
Set Com.ActiveConnection = con
Com.CommandText = "select HostUser, HostPassword from '" + DomainPath + "' WHERE objectClass='user' AND sAMAccountName='" + sAMAccountName + "'
Set rs = Com.Execute
if Not rs.EOF then
  HostUserName = rs.Fields("HostUserName")
  HostPassword = rs.Fields("HostPassword")
  if Len(HostUserName = 0) Then
     rc = msgbox("User name unknown" , vbokonly + vbExclamation, "Error")
  else
     ; We have the user name and password. Start the logon process
     Result = WaitForString("Welcome", 1, 38)
     if Result = True then
        Result = MoveTo(29,47)
        SendKeys HostUserName.Value
        Result = MoveTo(30,47)
        SendKeys HostPassword.Value
        SendKeys "<Enter>"
        Result = WaitForKbdUnlock()
        r = GetString(28, 2, 8)
        if r = "rejected" then
           rc = msgbox("User already logged in" , vbokonly + vbExclamation, "Error")
           Set QuickExit = True
        endif
     endif
  endif
endif
 
con.Close
rs = Nothing
Com = Nothing
con = Nothing
Domain = Nothing
jRoot = Nothing
End