Modify Home Drive & TS Home Drive AD Attributes

Posted on 2009-02-12
Last Modified: 2012-06-27
I am looking for a script to modify a number {not all} of users Home Drive  and TS Home Drive location.  {ie: we're moving their home drives}.

User names can be read from an Excell SS or .CSV file .TXT file - don't really care.

But I need to:
1) Read username
2) Set username attributes to update HomeDirectory and TSHomeDirectory..

Your timely response would be GREATLY appreciated.


Question by:MAJAEJ
    LVL 12

    Accepted Solution

    Hi there

    I have written a script for you to do what you want.
    You need to have the user logon names 1 per line in a text file somewhere

    There are settings you need to change in the script but all of them are between the borders. They should be self explanatory as to what you need to change them to.

    Save this script notepad as a .vbs file
    Run from the command prompt as
    CScript scriptName.vbs

    This si becuase it will output the results as we go along to the command window instead of dozens of popup prompts

    Hope it is what you are looking for


    Most people try to find the Terminal Services Attributes in ADSI and will never find them. This is due to the fact that all the settings for the Terminal Service Profile, Session, Remote Control and Environment tabs are all stored in a single attribute called User-Parameters. This attribute is a binary attribute and cannot be read using the normal ADSI tools.

    Since Windows 2003 Server there is a DLL file (TsUserEx.dll) that makes reading this attribute easy by binding to the User object first. Then we can read and write to this attribute via a wrapper which is the TsUserEx.dll.
    By installing the "Windows 2003 Server Administration Tools Pack" you will get this DLL installed.

    Option Explicit
    	Call SetTerminalServicesSettings
    Sub SetTerminalServicesSettings
    ' Version 1.0
    ' Written By Krystian Karia
    ' Dated 15-Feb-2009
    ' Reads  a list  of  user  logon  names from
    ' a text file  and then  searches  for  them
    ' in AD. We then check if  they  have a home
    ' Driectory  set  and  and TS Home Directory
    ' set and  report their current settings. If
    ' the Change Flag is set we also change them
    ' according  to  the preferences set between
    ' then borders below.
    ' Catch errors ourselves
    	On Error Resume Next 
    ' Declare Variables and Constants
    	Dim fso, oFile
    	Dim oRoot, objConnection, objCommand, objRecordSet, objUser
    	Dim strOldHome, strOldTSHome, strReadResults, arrUser
    	Dim blnChangeFlag, blnFileHasHeader, blnTSEnabled
    	Dim strHomeDirectory, strTSHomeDirectory
    	Dim strBase, strQuery, sFileLocation
    	Dim strHomeDrive, strTSHomeDrive
    	Dim i, INT_LINE_START
    	Const ForReading = 1
    '**************************************** PREFERENCES ****************************************
    ' <<<<< ONLY CHANGE SETTINGS BELOW (Keep the Quotes) >>>>>
    	sFileLocation = "C:\UserList.txt"					'<________Contains 1 user per line (must be their Logon Name)
    	blnChangeFlag = False								'<________True = Make the changes and False = Simulate changes
    	blnFileHasHeader = False							'<________True = UserList has a Header and False = UserList has no Header
    ' User Home Drive Settings
    	strHomeDrive = "H:"									'<________Set the Drive Letter to suit
    	strHomeDirectory = "\\FileSvr1\HomeDrives\"			'<________Server you currently have it - must end in \
    ' User Terminal Services Home Drive Settings
    	blnTSEnabled = 1									'<________1 = Enabled and 0 = Disabled 
     	strTSHomeDrive = "H:"								'<________Set the Drive Letter to suit
     	strTSHomeDirectory = "\\FileSvr1\TSHomeDrives\"		'<________Server you currently have it - must end in \
    ' <<<<< ONLY CHANGE SETTINGS ABOVE (Keep the Quotes) >>>>>
    '**************************************** PREFERENCES ****************************************
    ' Create Objects
    	Set fso = CreateObject("Scripting.FileSystemObject")
    ' Check Import UserList file exists
    	If fso.FileExists(sFileLocation) Then
    		Set oFile = fso.OpenTextFile(sFileLocation, ForReading, False)
    			If Err.Number <> 0 Then
    				ShowProgress "Error opening UserList: " & sFileLocation
    			End If
    		ShowProgress "UserList doesn't exist: " & sFileLocation
    	End If
    ' Read the whole contents of the File
    	Do While oFile.AtEndOfStream <> True
    		strReadResults = oFile.ReadAll
    	oFile.Close				' Close the file
    	Set oFile = Nothing		' Release the object from memory
    ' Create an array from contents of the file if not blank file
    	If Trim(strReadResults) <> "" Then
    		arrUser = Split(strReadResults, vbNewLine)
    	End If
    ' Is Header in file
    	If blnFileHasHeader = True Then
    		INT_LINE_START = 1
    		INT_LINE_START = 0
    	End If 
    ' Loop each user from the list in the file
    	For i = INT_LINE_START To UBound(arrUser)
    		If arrUser(i) = "" Then
    			Exit For
    		End If 
    		ShowProgress arrUser(i)
    ' Dynamically get the Domain you are currently in
    		Set oRoot = GetObject("LDAP://rootDSE")
    			strBase = oRoot.Get("defaultNamingContext")
    ' Open a connection to Active Directory Database
    		Set objConnection = CreateObject("ADODB.Connection")
    			objConnection.Open "Provider=ADsDSOObject;"
    		Set objCommand = CreateObject("ADODB.Command")
    			objCommand.ActiveConnection = objConnection
    				If Err.Number <> 0 Then
    					ShowProgress "Error opening a connection to AD"
    					ShowProgress Err.Number & ": " & Err.Description
    				End If
    			' Clear variable ready for re-use
    				strQuery = ""
    				strQuery = strQuery & "<LDAP://" & strBase & ">;"
    				strQuery = strQuery & "(&(objectCategory=person)(objectClass=user)"
    						strQuery = strQuery & "(sAMAccountName=" & arrUser(i) & "));"
    				strQuery = strQuery & "sAMAccountName,DisplayName,ADsPath,homeDirectory;"
    				strQuery = strQuery & "subtree"  
    			objCommand.CommandText = strQuery
    		Set objRecordSet = objCommand.Execute
    				If Err.Number <> 0 Then
    					ShowProgress "Error opening a Recordset to AD"
    					ShowProgress Err.Number & ": " & Err.Description
    				End If
    ' Check if any records were returned			
    		If objRecordSet.RecordCount = 0  Then ' User not found in AD
    			ShowProgress "No records found for user: " & arrUser(i)
    			Do Until objRecordset.EOF
    				Set objUser = GetObject(objRecordset.Fields("ADsPath"))
    					If Err.Number <> 0 Then
    						ShowProgress "Error binding to user: " & arrUser(i)
    						ShowProgress Err.Number & ": " & Err.Description
    					End If
    ' Check if a Home Directory exists
    				If Not IsNull(objRecordset.Fields("homeDirectory")) Then
    					strOldHome = objRecordset.Fields("homeDirectory")
    						ShowProgress "Old Home: " & strOldHome
    					ShowProgress "No Home Drive Exists for User: " & arrUser(i)
    				End If 'IsNull
    ' Check if a TSHome Directory exists
     				If Not IsNull(objUser.TerminalServicesHomeDirectory) Then
    					strOldTSHome = objUser.TerminalServicesHomeDirectory
    						ShowProgress "Old TS Home: " & strOldTSHome
     					ShowProgress "No TS Home Drive Exists for User: " & arrUser(i)
     				End If 'IsNull
    ' Make the changes if flag set to True
    				If blnChangeFlag = True Then 
    				' Change the Home Drive Settings
    					If strOldHome <> "" Then
    						objUser.Put "homeDirectory", strHomeDirectory & arrUser(i)
    						objUser.Put "homeDrive", strHomeDrive
    						objUser.Put "homeDirectory", strHomeDirectory & arrUser(i)
    					End If
    						If Err.Number <> 0 Then
    							ShowProgress "Error changing the Home Drive details"
    							ShowProgress Err.Number & ": " & Err.Description
    						End If
    				' Change the TS Home Drive Settings
    					If strOldTSHome <> "" Then
    						objUser.TerminalServicesHomeDirectory = strHomeDirectory & arrUser(i)
    						objUser.TerminalServicesHomeDrive = strHomeDrive
    						objUser.TerminalServicesHomeDirectory = strHomeDirectory & arrUser(i)
    						objUser.AllowLogon = blnTSEnabled
    					End If
    						If Err.Number <> 0 Then
    							ShowProgress "Error changing the TS Home Drive details"
    							ShowProgress Err.Number & ": " & Err.Description
    						End If
    					ShowProgress "Change not made - change the flag to True if you want changes to take effect"
    				End If 
    				Set objUser = Nothing
    		End If
    	Next ' i
    End Sub
    Sub ShowProgress(sComments)
    	WScript.Echo sComments
    End Sub

    Open in new window


    Author Closing Comment

    Wow - way more than I was expecting. Thank you.
    I'll take this apart many times over to learn from it I am sure.

    Thank you...
    LVL 12

    Expert Comment

    Hi, your welcome.
    Thanks for the grade and points.

    I wonder if the fact this post was left for so long was down to the Terminal Services attributes (or lack of them should i say). Even i was baffled for quite a while till i did some research. :-)

    Anyway, thanks

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    This is an addendum to the following article: Acitve Directory based Outlook Signature ( The script is fine, and works in normal client-server domains…
    Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    728 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

    21 Experts available now in Live!

    Get 1:1 Help Now