Solved

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

Posted on 2009-05-12
5
527 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A quick step-by-step overview of installing and configuring Carbonite Server Backup.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now