Option Explicit
Dim oConn
Dim oRS
Dim oSid
Dim strServerName, strGALEmail, strGALLastName, strGALFirstName, strGALDisplayName, strGALDescription, strGALCountry, strGALCity, strGALOffice
Dim strDomain, strGALZip, strGALState, strGALStreet, strGALTelNum, strGALTitle, strGALRank, oFSO, readFile, outFile, iUpperBound
Dim strUserName, oRootDSE, oConnection, oCommand, oRecordSet, strConnect, UserObj, strAD
Dim strSid
Dim strQuery
Public strUser
Const ADS_SID_HEXSTRING = 1
Const ADS_SID_WINNT_PATH = 5
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set readFile = oFSO.OpenTextFile("C:\documents and settings\desktop\users.txt")
Set outFile = oFSO.CreateTextFile("C:\documents and settings\desktop\UpdateADInfo.log", True)
Set oSid = CreateObject("ADsSid")
Set oConn = CreateObject("ADODB.Connection") 'Create an ADO Connection
oConn.Provider = "ADsDSOOBJECT" ' ADSI OLE-DB provider
oConn.Open "ADs Provider"
' Opens a textfile containg the usernames you want to pull from exchange and puts them in an array
iUpperBound = 0
While Not readFile.AtEndOfStream
ReDim Preserve arrUserNames(iUpperBound)
arrUserNames(UBound(arrUserNames)) = readFile.ReadLine
iUpperBound = iUpperBound + 1
Wend
readFile.Close
' Code that conects to the user's GAL info and puts it into variables
For Each strUser in arrUserNames
strUserName = strUser
On Error Resume Next
oSid.SetAs ADS_SID_WINNT_PATH, "WinNT://domain/" & strUser 'Get the user account SID
strSid = oSid.GetAs(ADS_SID_HEXSTRING)
If Err <> 0 Then
outFile.WriteLine "Could not find " & strUser & " in AD." & " " & Date & " " & Time
End If
On Error Goto 0
strQuery = "<LDAP://server>;(&(objectClass=person)(Assoc-NT-Account=" & strSid & "));" & _
"adspath,mail,sn,givenName,cn,description,co,l,physicalDeliveryOfficeName,postalCode,st,postaladdress,TelephoneNumber,title," & _
"Extension-Attribute-1;subtree"
Set oRS = oConn.Execute(strQuery)
If oRS.EOF And oRS.EOF Then
outFile.Writeline "Could not locate user " & strUser & "'s information in the GAL." & " " & Date & " " & Time
Else
strGALEmail = oRS.Fields("mail")
strGALLastName = oRS.Fields("sn")
strGALFirstName = oRS.Fields("givenName")
strGALDisplayName = oRS.Fields("cn")
strGALDescription = oRS.Fields("description")
strGALCountry = oRS.Fields("co")
strGALCity = oRS.Fields("l")
strGALOffice = oRS.Fields("physicalDeliveryOfficeName")
strGALZip = oRS.Fields("postalCode")
strGALState = oRS.Fields("st")
strGALStreet = oRS.Fields("postaladdress")
strGALTelNum = oRS.Fields("TelephoneNumber")
strGALTitle = oRS.Fields("title")
strGALRank = oRS.Fields("Extension-Attribute-1")
End If
' This line uses the username from above, connects to it in AD, and returns the full Distinguished Name
strConnect = fncConnectAD(strUserName)
' The following code connects to the user's account in AD and updates the account info with the info from the GAL
On Error Resume Next
Set UserObj = GetObject("LDAP://" & strConnect)
'On Error Goto 0
If Err <> 0 Then
outFile.Writeline "There was an error connecting to " & strUserName & "'s account in Active Directory." & " " & Date & " " & Time
Err.Clear
Else
UserObj.Put ("mail"), (strGALEmail)
UserObj.Put ("sn"), (strGALLastName)
UserObj.Put ("givenName"), (strGALFirstName)
UserObj.Put ("displayName"), (strGALDisplayName)
UserObj.Put ("description"), (strGALDisplayName)
'UserObj.Put ("description"), (strGALDescription)
UserObj.Put ("co"), (strGALCountry)
UserObj.Put ("l"), (strGALCity)
UserObj.Put ("physicalDeliveryOfficeName"), (strGALOffice)
UserObj.Put ("postalCode"), (strGALZip)
UserObj.Put ("st"), (strGALState)
UserObj.Put ("streetAddress"), (strGALStreet)
UserObj.Put ("telephoneNumber"), (strGALTelNum)
UserObj.Put ("title"), (strGALTitle)
UserObj.SetInfo
End If
On Error Goto 0
Next
wscript.echo "Done"
' This funtion will take a username and return the complete Distinguished Name
Public Function fncConnectAD(strUsername)
Set oRootDSE = GetObject("LDAP://rootDSE")
Set oConnection = CreateObject("ADODB.Connection")
oConnection.Open "Provider=ADsDSOObject;"
Set oCommand = CreateObject("ADODB.Command")
oCommand.ActiveConnection = oConnection
oCommand.CommandText = "<LDAP://" & oRootDSE.get("defaultNamingContext") & ">;(&(objectCategory=User)(samAccountName=" & _
strUserName & "));distinguishedName;subtree"
Set oRecordSet = oCommand.Execute
On Error Resume Next
fncConnectAD = oRecordSet.Fields("distinguishedName")
On Error Goto 0
End Function