Scripting.FileSystemObject
(1)
ObjTextFile.AtEndOfStream
(1)
ObjFSO.OpenTextFile
(1)
ObjUser.sAMAccountName
(1)
ObjTextFile.ReadLine
(1)
SAMAccountName
(1)
ObjUser.SetInfo
(1)
EnumOUs
(1)
Enumerating organisational units within vbs script
Asked By TJ_2006
06-Dec-07 01:58 AM

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
Enumerating organisational units within vbs script
Asked By Richard Mueller [MVP]
05-Dec-07 09:16 AM

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
--
Const ForReading = 1 Const ForAppending = 8 Const CONVERT_TO_LOCAL_TIME = True Set objFSO = CreateObject("Scripting.FileSystemObject") Set dtmStartDate = CreateObject("WbemScripting.SWbemDateTime") Set dtmEndDate = CreateObject("WbemScripting.SWbemDateTime dtmStartDate.SetVarDate DateToCheck, CONVERT_TO_LOCAL_TIME dtmEndDate.SetVarDate DateToCheck -7, CONVERT_TO_LOCAL_TIME Set objTextFile = objFSO.OpenTextFile _ ("C: \ servers.txt", ForReading) Set objTextFile1 = objFSO.OpenTextFile _ ("C: \ service_status.txt", ForAppending, True) Set objTextFile2 = objFSO OpenTextFile _ ("C: \ log_status.txt", ForAppending, True) Do Until objTextFile.AtEndOfStream
fix this? On Error Resume Next Const ForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile("c: \ scripts \ servers.txt", ForReading) Do Until objTextFile.AtEndOfStream strComputer = objTextFile.Readline ' = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ' Begin main code ' = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ' general constants This is being run servers & to learn how to script in VBS. New to scripting, I appreciate the help. cript" / > = 3D = 3D = 3D = 3D = 3D
error resume Next Const ForReading = 1 Const ForAppending = 8 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile _ ("C: \ servers.txt", ForReading) Set objTextFile1 = objFSO.OpenTextFile _ ("C: \ last_reboot.csv", ForAppending, True) Do Until objTextFile.AtEndOfStream strNextLine = objTextFile.Readline arrServiceList = Split(strNextLine , ", ") strComputer = arrServiceList(0) Set
On Error Resume Next Const ForReading = 1 Set objDictionary = CreateObject("Scripting.Dictionary") Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile("c: \ TEMP \ dry_servers.txt", ForReading) i = 0 Do Until objTextFile AtEndOfStream strNextLine = objTextFile.Readline objDictionary.Add i, strNextLine i = i + 1 Loop For
Windows Server Remote WMI scripting problems I am trying to create a back-up of please help? today = FormatDateTime(Now, vbLongDate) Const ForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile(" \ TEXT FILE.txt", ForReading) Do Until objTextFile.AtEndOfStream strComputer = objTextFile.Readline Set objFolder = objFSO.CreateFolder("NETWORK FOLDER" & strcomputer & " \ " & today & " \ ") Set
anyone can help sort it out. The error reads: Windows Scripting Host Script: c: \ remote_printer_cleanup_install.vbs Line: 39 Char: 45 Error ForReading = 1 Const WbemAuthenticationLevelPktPrivacy = 6 strUser = "Administrator" strPassword = "domainadminpassword" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile(". \ computers.txt", ForReading) Set objWbemLocator = CreateObject("WbemScripting.SWbemLocator") ' strComputerUser = "Computer vbTab & "Logged in User" Do Until objTextFile.AtEndOfStream strComputer = objTextFile.Readline ' = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ' Insert your code here ' = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Set objWMIService = objwbemLocator.ConnectServer (strComputer
do this? On Error Resume Next Const ForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile("c: \ scripts \ servers.txt", ForReading) Do Until objTextFile.AtEndOfStream strComputer = objTextFile.Readline Set objShell = CreateObject("WScript.Shell") strCommand = "%comspec% / c ping Set objExecObject = objShell.Exec(strCommand) Do While Not objExecObject.StdOut.AtEndOfStream strText = objExecObject.StdOut.ReadAll() If Instr(strText, "Reply") > 0 Then
10, Char:5, Error: Type mismatch Const ForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile _ ("c: \ test \ test2 \ scanlist.txt", ForReading) Do Until objTextFile.AtEndOfStream strNextline = objTextFile.readline strComputer = Split(strNextline , ", ") Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel the file has one NetBIOS name per line, use: strComputer = objTextFile.ReadLine If each line has comma delimited values, and the