Solved

Script to find and change users' with homedirectory path on one server to another server AD

Posted on 2009-05-12
5
532 Views
Last Modified: 2013-12-24
I need a script to find users in AD who have their homedirectory on a specific server, and then change that homedirectory to a new path.

E.g., I have many users who currently are using the path \\servername\username$.  We're moving the folders to a new server, and I only want to use one share.  So for those users, I'd like to find them and change the path to \\newservername\users$\%username%.

If I have to, I can do it through ADUC by selecting them and modifying them, but I'd rather script it.
I've already looked through a lot of scripting guys scripts, and can't seem to put the two together (Find and then replace).
0
Comment
Question by:zelron22
[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
  • 4
5 Comments
 
LVL 2

Expert Comment

by:CBalderson
ID: 24366743
Hi, give this a try.

Save as a .VBS file and Test first
Change the values based on your environment.  


 
' --------------------------------------------------------------' 
' UserHomeDir .vbs
' Change user's HomeFolder in a named OU (find and replace)
' May 2009
' --------------------------------------------------------------' 
Option Explicit
Dim objOU, objUser, objRootDSE
Dim strContainer, strDNSDomain
Dim strOldPath, strNewPath, strUser, strhomeDirectory
Dim count
 
' Bind to Active Directory Domain
Set objRootDSE = GetObject("LDAP://RootDSE") 
strDNSDomain = objRootDSE.Get("DefaultNamingContext") 
 
' -------------------------------------------------------------'
' Important change OU= to reflect your domain
' -------------------------------------------------------------'
strContainer = "OU=Users,OU=Test,"
strOldPath = "\\test\Users\"
strNewPath = "\\NewServer\Users\"
strContainer = strContainer & strDNSDomain
 
' Loop through OU=, Find Users that Match
set objOU =GetObject("LDAP://" & strContainer )
count=0
For each objUser in objOU
If objUser.class="user" then
	If INSTR(UCASE(objUser.homeDirectory),UCASE(strOldPath)) then
		'wscript.echo objUser.name
		ChangeHomeDir()
		count = count +1
	End If
End If
Next 
 
wscript.echo "Processed Users = " & count
 
Function ChangeHomeDir()
'Change User homeDirectory to strNewPath
	strUser = objUser.sAMAccountName
	strHomeDirectory = strNewPath & strUser
	objUser.homeDirectory = strHomeDirectory
	objUser.SetInfo
End Function

Open in new window

0
 
LVL 15

Author Comment

by:zelron22
ID: 24368142
This is great, but I would like to find them by the homedirectory they already have, vs. an OU that they're in.
0
 
LVL 2

Expert Comment

by:CBalderson
ID: 24368206
The OU is a filter to speed things up and to help you isolate a few targets before modifying the entire domain.  
The script does a find and replace based on the homeDirectory value.

You should be able to remove the line
             strContainer = "OU=Users,OU=Test,"
The result would be a query of you entire domain for homeDirectory value match strOldPath and replace them with the strNewPath.

Hope that helps
0
 
LVL 2

Expert Comment

by:CBalderson
ID: 24368259
Hold on a sec, your old path is \\servername\username$ we can make a few quick modifications to sort that.  I don't have a test for this version but it should do the trick - give it a test run.

 
' --------------------------------------------------------------' 
' UserHomeDir .vbs
' Change user's HomeFolder in a named OU (find and replace)
' May 2009
' --------------------------------------------------------------' 
Option Explicit
Dim objOU, objUser, objRootDSE
Dim strContainer, strDNSDomain
Dim strOldPath, strNewPath, strUser, strhomeDirectory
Dim count
 
' Bind to Active Directory Domain
Set objRootDSE = GetObject("LDAP://RootDSE") 
strDNSDomain = objRootDSE.Get("DefaultNamingContext") 
 
' -------------------------------------------------------------'
' Important change OU= to reflect your domain
' -------------------------------------------------------------'
strContainer = "OU=Users,OU=Brussels,"
strOldPath = "\\serversname\"
strNewPath = "\\servername\Users$\"
strContainer = strContainer & strDNSDomain
 
' Loop through OU=, Find Users that Match
set objOU =GetObject("LDAP://" & strContainer )
count=0
For each objUser in objOU
If objUser.class="user" then
	If UCASE(objUser.homeDirectory)=UCASE(strOldPath & objUser.sAMAccountName & "$")) then
		'wscript.echo objUser.name
		ChangeHomeDir()
		count = count +1
	End If
End If
Next 
 
wscript.echo "Processed Users = " & count
 
Function ChangeHomeDir()
'Change User homeDirectory to strNewPath
	strUser = objUser.sAMAccountName
	strHomeDirectory = strNewPath & strUser
	objUser.homeDirectory = strHomeDirectory
	objUser.SetInfo
End Function

Open in new window

0
 
LVL 2

Accepted Solution

by:
CBalderson earned 500 total points
ID: 24375206
Ok, now you are getting complicated!  :-)
Lets try to set up a loop using an array... paste the snippet below in you Excel VB Editor
Sub GetHR_Data()
'Add as many OU's as you need to query
strOU = Array("OU=Users,OU=PARENT1,", "OU=Users,OU=PARENT2,", "OU=Users,OU=PARENT3,")
strSheetName = "HR"
 
'Select Sheet to write data to
Sheets(strSheetName).Select
 
'Clear Data
Columns("A:A").Select
Selection.ClearContents
Range("A1").Select
 
'Call AD for as many arguments that exist in the strOU list/array
ArrLength = UBound(strOU) + 1
For i = 1 To ArrLength
Row = Sheets(strSheetName).UsedRange.Rows.count
Debug.Print usedRowsCount
result = GetAD_Info(strOU(i - 1), Row)
Next i
 
End Sub
Function GetAD_Info(x, y)
' Bind to Active Directory Domain
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("DefaultNamingContext")
 
strContainer = x & strDNSDomain
Row = y
 
Set objOU = GetObject("LDAP://" & strContainer)
 
'Get AD Data and write to sheet
For Each objUser In objOU
    If objUser.class = "user" Then
        'Debug.Print objUser.cn
        Cells(Row, 1) = objUser.cn
        Row = Row + 1
    End If
Next
 
End Function

Open in new window

0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

751 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