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

x
?
Solved

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

Posted on 2009-05-12
5
Medium Priority
?
540 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
  • 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
What we learned in Webroot's webinar on multi-vector protection.
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

886 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