Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Change HomeDirectory for all Active Directory users in a OU

Posted on 2011-02-25
8
Medium Priority
?
1,016 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

Had a business requirement to store the mobile number in an environmental variable. This is just a quick article on how this was done.
This process allows computer passwords to be managed and secured without using LAPS. This is an improvement on an existing process, enhanced to store password encrypted, instead of clear-text files within SQL
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 video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

704 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