Solved

Change HomeDirectory for all Active Directory users in a OU

Posted on 2011-02-25
8
1,012 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Resolve DNS query failed errors for Exchange
Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 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