[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Modify Home Drive & TS Home Drive AD Attributes

Posted on 2009-02-12
3
Medium Priority
?
1,345 Views
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.

Thanks

MAJ
0
Comment
Question by:MAJAEJ
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
Krys_K earned 2000 total points
ID: 23646378
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

Regards
Krystian

P.S.
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.
http://www.microsoft.com/downloads/details.aspx?FamilyID=c16ae515-c8f4-47ef-a1e4-a8dcbacff8e3&DisplayLang=en

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
				WScript.Quit
			End If
	Else
		ShowProgress "UserList doesn't exist: " & sFileLocation
		WScript.Quit
	End If
 
' Read the whole contents of the File
	Do While oFile.AtEndOfStream <> True
		strReadResults = oFile.ReadAll
	Loop
	
	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
	Else
		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
					WScript.Quit
				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
					WScript.Quit
				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)
		Else
			
			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
						Err.Clear
					End If
 
' Check if a Home Directory exists
				If Not IsNull(objRecordset.Fields("homeDirectory")) Then
					strOldHome = objRecordset.Fields("homeDirectory")
						ShowProgress "Old Home: " & strOldHome
				Else
					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
 				Else
 					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.SetInfo
					Else
						objUser.Put "homeDrive", strHomeDrive
						objUser.Put "homeDirectory", strHomeDirectory & arrUser(i)
						objUser.SetInfo
					End If
 
						If Err.Number <> 0 Then
							ShowProgress "Error changing the Home Drive details"
							ShowProgress Err.Number & ": " & Err.Description
							Err.Clear
						End If
 
 
				' Change the TS Home Drive Settings
					If strOldTSHome <> "" Then
						objUser.TerminalServicesHomeDirectory = strHomeDirectory & arrUser(i)
						objUser.SetInfo
					Else
						objUser.TerminalServicesHomeDrive = strHomeDrive
						objUser.TerminalServicesHomeDirectory = strHomeDirectory & arrUser(i)
						objUser.AllowLogon = blnTSEnabled
						objUser.SetInfo
					End If
 
						If Err.Number <> 0 Then
							ShowProgress "Error changing the TS Home Drive details"
							ShowProgress Err.Number & ": " & Err.Description
							Err.Clear
						End If
 
 
				Else
					ShowProgress "Change not made - change the flag to True if you want changes to take effect"
				End If 
					
					objRecordset.MoveNext
				Set objUser = Nothing
			Loop
		End If
 
	Next ' i
 
End Sub
 
 
Sub ShowProgress(sComments)
 
	WScript.Echo sComments
 
End Sub

Open in new window

0
 

Author Closing Comment

by:MAJAEJ
ID: 31546325
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...
0
 
LVL 12

Expert Comment

by:Krys_K
ID: 23659229
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
Krystian
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

834 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