Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Change HomeDirectory for all Active Directory users in a OU

Posted on 2011-02-25
8
Medium Priority
?
1,020 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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
LVL 74

Accepted Solution

by:
Glen Knight earned 2000 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

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office 365 is a subscriptions based service which includes services like Exchange Online and Skype for business Online. These services integrate with Microsoft's online version of Active Directory called Azure Active Directory.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

971 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