Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can anyone make this work? (AD Binding) 1

Status
Not open for further replies.

keybrdcowboy

IS-IT--Management
Aug 31, 2004
96
US
This script is pulling a username from a textfile, connecting to our Exchange 5.5 server and pulling out info from there, then is supposed to connect back to that same user in our AD. I cannot get it to bind back to that user after making the connection with Exchange. Can anyone help me out here? Thanks.

Code:
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, strCount
Dim strTest, objRootDSE, objItem, strUserName
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\me\desktop\lacklandusers.txt")
Set outFile = oFSO.CreateTextFile("C:\documents and settings\me\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 coonects to the user's GAL info and puts it into variables
For Each strUser in arrUserNames
  'wscript.echo strUser
  strUserName = strUser
  oSid.SetAs ADS_SID_WINNT_PATH, "WinNT://mydomain/" & strUser  'Get the user account SID 
  strSid = oSid.GetAs(ADS_SID_HEXSTRING)
  strQuery = "<LDAP://myserver>;(&(objectClass=person)(Assoc-NT-Account=" & strSid & "));" & _
           "adspath,mail,sn,givenName,cn,description,co,l,physicalDeliveryOfficeName,postalCode,st,street,Telephone-Number,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("street")
    strGALTelNum = oRS.Fields("Telephone-Number")
    strGALTitle = oRS.Fields("title")
    strGALRank = oRS.Fields("Extension-Attribute-1")
  End If
Next

'wscript.echo strUserName
'strTest = ",fulldn,"
'set objRootDSE = GetObject("LDAP://RootDSE")
'set objItem = GetObject("LDAP://cn=" & strUserName & strTest & objRootDSE.Get("DefaultNamingContext"))
'wscript.echo objItem.sn


set objItem = GetObject("WinNT://mydomain/" & strUsername & ",user")
'set objItem = GetObject("WinNT://myserver/" & strUsername)
'set strCRUser = GetObject("LDAP://mydomain/" & strUserName & "")
'Set objRootDSE = GetObject("LDAP://mydomain/RootDSE")
'Set objItem = GetObject("LDAP://" & strUserName & objRootDSE.Get("defaultNamingContext"))
'Set objItem = GetObject("LDAP://cn=" & strUserName & "," & objRootDSE.Get("defaultNamingContext"))


wscript.echo objItem.get("mail")

'Set oSid = CreateObject("ADsSid")  'from ResourceKit
'oSid.SetAs ADS_SID_WINNT_PATH, "WinNT://" & strDomain & "/" & strUsername  'Get the user account SID
'strSid = oSid.GetAs(ADS_SID_HEXSTRING)  'Convert to binary string

'Clean Up
'Set oSid = Nothing
'oRS.Close
'oConn.Close
'Set oRS = Nothing
'Set oConn = Nothing

'wscript.quit
You can see all the stuff I have tried towards the end of my code... any help would be great. Thanks.
 
Can you tell where starts not working? Here??? onward?
> set objItem = GetObject("WinNT://mydomain/" & strUsername & ",user")

 
Actually, that part works, but when I do that, I can't access the personal information on the account, such as First Name, Last Name, Email, etc. I can do name, which will display the full name, but I guess I need to do an LDAP binding so taht I can access all the account attributes. Thanks.
 
Still cannot figure what real message you want to convey. But maybe you solve it or ... It doesn't matter.
 
Okay, the script intially pulls a username from a text file and then connects to that user in our exchange server. From there it pulls out all the personal information listed in exchange, such as first name, last name, email address, city, state, etc. It then assigns all of that information to variables. Now I want to connect to that same user in our active directory and update the AD info with the information that I just pulled out of exchange. I can get it to pull all the information from exchange and store it in variables, but I cannot get it to connect back to that user in AD in order to update that information. Just a binding issue. Please let me know if you need any more info. Thanks.
 
Okay, looking at that, will I be able to put information in the account? I don't see how a .Put would work there. Any enlightenment? Thanks...
 
You asked for help on binding to the user object. The function for retrieving the Distinguished name will help you with that.

A quick example:
Code:
Username = "Joe"
[green]'Call the function to get the Distinguished name[/green]
DN = SearchDistinguishedName(Username)
[green]'Now bind to the user object[/green]
Set UserObj = GetObject(DN)

I hope you find this post helpful.

Regards,

Mark
 
I get an invalid procedure call on the "Set UserObj = GetObject(DN)" ..... anymore help? I appreciate it so far...
 
Okay, now I have it almost working. I am now getting a syntax error on that same line....
 
Okay, I got it working fine now. Just one more question... Is this the most efficient way to do this? Is there a way to do it without using the function? Thanks for all your help.
 
You could move the function code into the script, or you could look at using the naemspace object. There is always more than one way to do things, but you won't see a performance hit with this.

I hope you find this post helpful.

Regards,

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top