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

ADSI OU help 1

Status
Not open for further replies.

Brycspain

IS-IT--Management
Mar 9, 2006
150
US
Hello,

I'm trying to enumerate a set of users in a particular OU and then modify a property in every account. The problem I'm having is I can only pull the Canonical name and I need the sAMAccountName in order to bind to their account. I realize the user.cn returns the Canonical name, is there a property that returns the sAMAccountName instead? Any help would be appreciated...thanks

Code:
Option Explicit
Dim defaultnamingcontext, DeskAuth, objOU, user, userLDAP

DeskAuth = "SLogic"

defaultnamingcontext = (GetObject("LDAP://rootDSE")).Get("defaultNamingContext")

Set objOU = GetObject("LDAP://OU=Users-IS Dept, OU=Users-All," & defaultNamingContext)
objOU.Filter = Array("user")

For Each User In objOU
[b]userLDAP = SearchDistinguishedName(User.cn)[/b]
Set objUser = GetObject("LDAP://" & userLDAP)
objUser.Put "scriptPath", DeskAuth
objUser.SetInfo

Next
wscript.echo "Script Finished"

Function SearchDistinguishedName( vSAN )            
	On Error Resume Next
	Dim oRootDSE, oConnection, oCommand, oRecordSet            
	defaultNamingContext = GetObject("LDAP://rootDSE").Get("defaultNamingContext")        
	Set oConnection = CreateObject("ADODB.Connection")        
	oConnection.Open "Provider=ADsDSOObject;"        
	'On Error GoTo 0
	Set oCommand = CreateObject("ADODB.Command")        
	oCommand.ActiveConnection = oConnection        
	oCommand.CommandText = "<LDAP://" & defaultnamingcontext & ">;" _                            
	& "(&(objectCategory=User)" _                            
	& "(samAccountName=" & vSAN & "));" _                            
	& "distinguishedName;subtree"        
	Set oRecordSet = oCommand.Execute        
	' you must use a for/next here or you will get errors    
	' a recordset is always a collection    
	While Not oRecordSet.EOF        
       SearchDistinguishedName = oRecordSet.Fields("DistinguishedName")         
	oRecordset.MoveNext       
	Wend       
	oConnection.Close    
End Function
 
it should simply be

User.SamAccountName

if that doesn't work try to see if you can get

User.ADSPath

if that works then you can do

...code
Set objUser = GetObject(User.ADSPath)
...code



--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
>I realize the user.cn returns the Canonical name
Just a precision: user.cn, it is called common-name. Canonical name is a technical term and it means something else.
 
Thanks DM...I learned something new.

Good point Tsuji, thanks for letting me know.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top