Jump to content

Enumerating organisational units within vbs script


Recommended Posts

Guest TJ_2006
Posted

Hi,

 

I have developed alot of vbs logon scripts and administrate a medium

size network, we are just about to make certain users have "roaming

profiles", i have created the script below to read a list of account

names and set there profile path to do so the only problem is i have

to hardcode the organisational unit which the user is in, what i would

like to do is enumerate all containers so regardless of what ou the

user sits in it will update the profile.

 

 

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objTextFile = objFSO.OpenTextFile _

("roamingprofiles.txt", ForReading)

 

Do Until objTextFile.AtEndOfStream

AccountName = objTextFile.ReadLine

Set objUser = GetObject _

("LDAP://CN=" & AccountName & ",OU=staff,DC=my domain,DC=com")

 

objUser.Put "profilePath", "\\storage\profiles\" & accountName

 

wscript.echo "profile set for " & accountName & " to \\server\share\"

& accountName

 

objUser.SetInfo

Loop

 

' end of script

 

Any help would be greatly appreciated.

 

kindest regards James

  • Replies 1
  • Created
  • Last Reply

Popular Days

Guest Richard Mueller [MVP]
Posted

Re: Enumerating organisational units within vbs script

 

TJ wrote:

> I have developed alot of vbs logon scripts and administrate a medium

> size network, we are just about to make certain users have "roaming

> profiles", i have created the script below to read a list of account

> names and set there profile path to do so the only problem is i have

> to hardcode the organisational unit which the user is in, what i would

> like to do is enumerate all containers so regardless of what ou the

> user sits in it will update the profile.

>

>

> Const ForReading = 1

> Set objFSO = CreateObject("Scripting.FileSystemObject")

> Set objTextFile = objFSO.OpenTextFile _

> ("roamingprofiles.txt", ForReading)

>

> Do Until objTextFile.AtEndOfStream

> AccountName = objTextFile.ReadLine

> Set objUser = GetObject _

> ("LDAP://CN=" & AccountName & ",OU=staff,DC=my domain,DC=com")

>

> objUser.Put "profilePath", "\\storage\profiles\" & accountName

>

> wscript.echo "profile set for " & accountName & " to \\server\share\"

> & accountName

>

> objUser.SetInfo

> Loop

>

> ' end of script

>

> Any help would be greatly appreciated.

 

There are two approaches. One is a recursive function to enumerate OU's,

similar to:

==========

Dim objDomain

 

' Bind to domain object.

Set objDomain = GetObject(LDAP://dc=MyDomain,dc=com)

 

Call EnumOUs(objDomain)

 

Sub EnumOUs(objParent)

' Recursive sub to enumerate OU's.

Dim objUser, objChild

 

' First, enumerate users in the Parent.

objParent.Filter = Array("user")

For Each objUser In objParent

' Do something with each user.

objUser.Put "profilePath", \\storage\profiles\ _

& objUser.sAMAccountName

objUser.SetInfo

Next

 

' Filter on child OU's and containers.

objParent.Filter = Array("organizationalUnit", "container")

' Enumerate child OU's.

For Each objChild In objParent

' Call this sub recursively.

Call EnumOUs(objChild)

Next

End Sub

=========

The sAMAccountName attribute is the "pre-Windows 2000 logon name", which is

probably what you mean by account name.

 

Another approach is to use ADO to retrieve information on all users in the

domain. See this link for details:

 

http://www.rlmueller.net/ADOSearchTips.htm

 

However ADO cannot be used to modify attributes directly, so you must

retrieve the distinguishedNames of the users and use this bind to the user

object to make changes.

 

--

Richard Mueller

Microsoft MVP Scripting and ADSI

Hilltop Lab - http://www.rlmueller.net

--


×
×
  • Create New...