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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Active Directory, modifying all existing users 1

Status
Not open for further replies.

Trevoke

Programmer
Jun 6, 2002
1,142
US
I would like to create a script which is meant to run only once and update all the 'mail' fields (for the user email address). I have the bit prepared for how to generate the email address from the user's name, and I got as far as this:

Code:
On Error Resume Next



Const ADS_SCOPE_SUBTREE = 2

Set objRootDSE = GetObject ("LDAP://RootDSE")
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
objCommand.CommandText = _
    "SELECT Name, givenName, sn, mail, ou, cn FROM 'LDAP://dc=[removed],dc=[removed]' WHERE objectCategory='user'"  
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst
Do Until objRecordSet.EOF
    'Wscript.Echo objRecordSet.Fields("Name").Value
    Set email = '' This tidbit removed for security ''
    
    Set objUser = GetObject("LDAP://cn=" & objRecordSet.Fields("Name").value & "," & _ 
        objRootDSE.Get("DefaultNamingContext"))
    objUser.Put "mail", email
    objUser.SetInfo
    'wscript.echo objrecordset.fields("mail").value
    objRecordSet.MoveNext
Loop

My issue is that the recordset is, well, just a recordset - I can't figure out how to get information from this query which is relevant enough to connect to that particular user object and make the modification.

Am I thinking about this totally incorrectly, or just missing some critical bit of knowledge?

Thanks!

Tao Te Ching Discussions : Chapter 9 (includes links to previous chapters)
What is the nature of conflict?
 
[1]
>objCommand.CommandText = _
"SELECT Name, givenName, sn, mail, ou, cn FROM 'LDAP://dc=[removed],dc=[removed]' WHERE objectCategory='user'"

[tt]objCommand.CommandText = _
"SELECT Name, givenName, sn, mail, ou, cn, [blue]adspath[/blue] FROM 'LDAP://dc=[removed],dc=[removed]' WHERE objectCategory='user'" [/tt]
[2]
>Set objUser = GetObject("LDAP://cn=" & objRecordSet.Fields("Name").value & "," & _
> objRootDSE.Get("DefaultNamingContext"))
[tt]Set objUser = GetObject([blue]RecordSet.Fields("adspath").value[/blue])[/tt]

 
That doesn't seem to be working..

objCommand.CommandText = "SELECT Name, givenName, sn, mail, adspath FROM 'LDAP://dc=domain,dc=com' WHERE objectCategory='user'"
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst
wscript.echo = GetObject(RecordSet.Fields("adspath").value)
Do Until objRecordSet.EOF
'Wscript.Echo objRecordSet.Fields("Name").Value
Set email = 'email address

Set objUser = GetObject(RecordSet.Fields("adspath").value)
objUser.Put "mail", email
objUser.SetInfo
'wscript.echo objUser.fields("mail").value
objRecordSet.MoveNext
Loop

That's right though, isn't it?

Tao Te Ching Discussions : Chapter 9 (includes links to previous chapters)
What is the nature of conflict?
 
>Set email = 'email address
What is it actually? I hope the appearance of Set keyword there is figurative, meaning setting some data to email variable, not literally meant to be there?
 
Set email = Lcase(Left(objRecordSet.Fields("givenName").value, 1) + objRecordSet.Fields("sn").value + "@mydomain.com")

Tao Te Ching Discussions : Chapter 9 (includes links to previous chapters)
What is the nature of conflict?
 
Done, and still no dice.
Do I need to define 'email' to exist before I can do something like that?

Also, the changes will be effective on the domain controller I use to authenticate and then trickle down, correct?

Tao Te Ching Discussions : Chapter 9 (includes links to previous chapters)
What is the nature of conflict?
 
>wscript.echo = GetObject(RecordSet.Fields("adspath").value)
What is this? If you want to inspect the value, it would be this.
[tt]wscript.echo RecordSet.Fields("adspath").value[/tt]
 
Also I wish the typo in my original post did not get the way that far, as it is so obvious a typo! That line should be read like this.
[tt]Set objUser = GetObject([red]obj[/red]RecordSet.Fields("adspath").value)[/tt]
 
Hey... That did it!
Thank you.
Obviously, I am very, very new to all this, and I am making the mistake of coding before learning.. But this allows the IT department to go a little further forward still :)
I should have read your corrections a few times but did not ses anything wrong with it.

For some reason, still no output from the first wscript.echo though..

Tao Te Ching Discussions : Chapter 9 (includes links to previous chapters)
What is the nature of conflict?
 
>For some reason, still no output from the first wscript.echo though..
Same typo is propagated there, is it not? (objRecordSet instead of RecordSet).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top