Solved

Change HomeDirectory for all Active Directory users in a OU

Posted on 2011-02-25
8
1,010 Views
Last Modified: 2012-05-11
Hello,

I have DC 2008 Server, with Active Directory. There is a OU "Users" where all users of my domain are defined.
For the users HomeDirectory there is :

"\\fileserver01\users\firstname.lastname".

The problem is I want to modify the HomeDirectory for all users with this :

"\\fileserver02\users\%username%".

How can I change for all users with a vbs. script ?

Thanks for your help.
0
Comment
Question by:croustimiel
8 Comments
 
LVL 74

Expert Comment

by:Glen Knight
ID: 34983171
Do you actually need to do it with a Vb Script?
you can actually just bulk select the users in Active Directory users and computers and then right click properties.

You can enter the variable in there and it will update all users.

Alternatively you can use ADModify: http://admodify.codeplex.com/
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 34983207
Another question, Is it just the attribute you need changed or do you also need to change the folder name?
0
 

Author Comment

by:croustimiel
ID: 34986169
I have 200 users, i don't want to make right click > prpperties on each users......
That's the reason why i ask a script.
0
 
LVL 74

Accepted Solution

by:
Glen Knight earned 500 total points
ID: 34986233
You don't need to right click each user, you can select them all (click the. Fist one, then press and hold the sift button, then click the last one) and then right click and select properties.

Enter the veritable and this will change all of them.

Vie done it literally hundreds of times.

Also ADModify will help with this kind of task
0
 

Author Comment

by:croustimiel
ID: 34986299
If I select all and just change the Homedirectory, the LogonScript specified in the same properties tab will be unchanged, because it is different for each users ???
0
 
LVL 74

Expert Comment

by:Glen Knight
ID: 34986308
Only the properties you change will be applied
0
 
LVL 74

Expert Comment

by:Glen Knight
ID: 34986311
Try it on 2 or 3 if you are unsure.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34993157
Hi, this script should do the job for you as well.  For testing purposes, make sure you comment out these lines:

      Set objUser = GetObject(adoRecordset.Fields("adsPath").Value)
      objUser.homeDirectory = strNewHomeDirectory & strUserName
      objUser.SetInfo
      Set objUser = Nothing


by putting an apostrophe in front of each line.  This will prevent any changing of the user accounts, while still reporting on them.

Regards,

Rob.
strOutputfile = "Users_HomeDirectoryChange.csv"

If Right(LCase(WScript.FullName), 11) = "wscript.exe" Then
	Set objShell = CreateObject("WScript.Shell")
	objShell.Run "cmd /k cscript """ & WScript.ScriptFullName & """", 1, False
	Set objShell = Nothing
	WScript.Quit
End If

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection

 ' Search entire Active Directory domain.
Set objRootDSE = GetObject("LDAP://RootDSE")

strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"

strDetails = """User Name"",""Full Name"",""AdsPath"",""Old Home Directory"",""New Home Directory"""

strOldHomeDirectory = "\\fileserver01\users\"
strNewHomeDirectory = "\\fileserver02\users\"
strFilter = "(&(objectCategory=person)(objectClass=user)(homeDirectory=" & Replace(strOldHomeDirectory, "\", "\5c") & "*))"

' Comma delimited list of attribute values to retrieve.
strAttributes = "samAccountName,cn,adsPath,homeDirectory"

' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False

' Run the query.
Set adoRecordset = adoCommand.Execute
 
' Enumerate the resulting recordset.
Do Until adoRecordset.EOF
    ' Retrieve values and display.
	strUserName = adoRecordset.Fields("samAccountName").Value
	If strUserName = "rsampson" Then
	strFullName = adoRecordset.Fields("cn").Value
	strOUPath = Replace(adoRecordset.Fields("adsPath").Value, "LDAP://CN=" & strFullName & ",", "")
	strOldHomeDirectory = adoRecordset.Fields("homeDirectory").Value
	WScript.Echo "Processing " & strFullName
	Set objUser = GetObject(adoRecordset.Fields("adsPath").Value)
	objUser.homeDirectory = strNewHomeDirectory & strUserName
	objUser.SetInfo
	Set objUser = Nothing
	strDetails = strDetails & VbCrLf &_
		"""" & strUserName & """" &_
		",""" & strFullName & """" &_
		",""" & strOUPath & """" &_
		",""" & strOldHomeDirectory & """" &_
		",""" & strNewHomeDirectory & strUserName & """"
	End If
    ' Move to the next record in the recordset.
    adoRecordset.MoveNext
Loop

' Clean up.
adoRecordset.Close
Set adoRecordset = Nothing

adoConnection.Close

Set objOutputFile = objFSO.CreateTextFile(strOutputFile, True)
objOutputFile.Write strDetails
objOutputFile.Close
Set objOutputFile = Nothing

WScript.Echo "Done. Please see " & strOutputFile
MsgBox "Done. Please see " & strOutputFile

Open in new window

0

Join & Write a Comment

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now