'Reference MS KB 839262 for SBS 2003(all versions)
'This script corrects a regkey in SBS 2003 
'as documented by that KB, performing the
'suggested changed for you. By SBS2003 defaults,
'the key has the wrong data type when installed.
'This script illustrates how to correct the data
'type and value with a script. After a warning,
'it deletes the existing key, creates a new one, 
'with a summary of the results displayed on completion.
'*************************************************
'by: Jeff Middleton SBS-MVP jeff@cfisolutions.com
'VBscript 5.6 or higher recommended, but not required
'*************************************************
'WARNING - This script is provided without warranty. 
'Use at your discretion, at your own risk--in all cases.
'Take caution and test if you alter this script.
'Properly modified, this script can be used for
'other regkey purposes. However, improperly changed,
'a similar script could delete huge sections
'of registry resulting in your entire system 
'being unusable without restore of a backup or 
'registry repair. 
'*************************************************

Dim WshShell
Dim sStatus, sRegPath, sRegName, sRegValue, sRegType, sResult, bResult  

'------- Set key change values
sRegPath  = "HKLM\SYSTEM\CurrentControlSet\Control\" 
sRegName  = "WaitToKillServiceTimeout"
sRegValue = "120000"
sRegType  = "REG_SZ"
'------- end

'or run the command below repeatly with values entered directly

bResult = ReCreateRegKey (sRegPath, sRegName, sRegValue, sRegType)
msgbox sStatus, vbOkonly, sResult


'	Syntax information for above
'		HKEY_CURRENT_USER 	= HKCU 
'		HKEY_LOCAL_MACHINE 	= HKLM 
'		HKEY_CLASSES_ROOT 	= HKCR 
'		HKEY_USERS 			= HKEY_USERS 
'		HKEY_CURRENT_CONFIG = HKEY_CURRENT_CONFIG 
'		
'		The four possible data types you can specify with strType are listed in the following table.
'		
'		Type Description In the Form of 
'		REG_SZ 		A string 								(A string)
'		REG_DWORD 		A number 								(An integer)
'		REG_BINARY 		A binary value 							(An integer, 0 or 1)
'		REG_EXPAND_SZ 	An expandable string (e.g., "%windir%\\calc.exe") 	(A string )
'		
'		Note:  The REG_MULTI_SZ type is not supported for the RegWrite method, therefore not here either.


Function ReCreateRegKey(sKeyPath, sKeyName, sKeyValue, sKeyType)
	Dim sFullKey, bErrTrap
	sStatus = ""
	sResult = ""
	sFullKey  = sKeypath & sKeyname
	Set WshShell = WScript.CreateObject("WScript.Shell")

	subStatus "Keypath:" , skeypath 
	subStatus "Keyname:" , sKeyname  & vbCrlf  
	
	'look for existing key, remove if found
	On Error Resume Next	
	bErrTrap = False
	sPreviousKeyValue = WshShell.RegRead(sFullKey)

	WarnToAbortChanges
	
	If err.number <> 0 Then
		subStatus "Existing:" , "Regkey currently missing, or access denied"
		err.clear
	Else
		subStatus "Existing:" , "Existing Regkey found = " & sPreviousKeyValue 
		WshShell.RegDelete sFullKey 
		If err.number <> 0 Then
			subStatus "Clearing:" , "Regkey found, could not be not deleted"
			bErrTrap = true
			err.clear
		Else
			subStatus "Clearing:" , "Existing Regkey successfully removed"
		End If
	End If
	 
	' bErrTrap (true)= key delete failed
	' bErrTrap (false)= key wasn't there, or permission denied before err cleared 
	
	If bErrTrap Then
		subStatus "Failed:" , "RegKey access denied. Aborting."
		sResult = "Failed: Error Access/Removing Regkey"
		ReCreateRegKey = False
		err.clear
		'End of processing: No Access
	Else
		subStatus "Proceeding:" , "Attempt to create new Regkey"
		WshShell.RegWrite sFullKey , sKeyValue, sKeyType 
		If err.number <> 0 Then
			subStatus "Error:" , "Writing Regkey not created, permission issue"
			err.clear
		Else 
			subStatus "Verifying:" , "Checking for new key"
			WshShell.RegRead sFullKey 
			If err.number <> 0 Then
				subStatus "Error:" , "Regkey not found, or permission denied"
				sResult = "Failed Access/Removing Regkey"
				ReCreateRegKey = False
				err.clear
			Else
				subStatus "Confirmed:" , sKeyname & " = " &  WshShell.RegRead(sFullKey)
				sResult = "Success: Regkey change completed"
				ReCreateRegKey = True
			End If
		End If
	End If

End Function

Sub WarnToAbortChanges

	sWarningHeader = "To delete, then create, the registry section indicated, choose OK. " & vbCrlf & vbCrlf
	sWarningFooter = "WARNING: Microsoft always recommends you retain a registry " & vbCrlf & _ 
				"backup before commiting to any registry changes."  & vbCrlf & vbCrlf & _ 
				"Click Cancel to abort without changes to this location:" & vbCrlf & vbCrlf 
	
	If Not msgbox(sWarningHeader & sStatus & sWarningFooter , vbOkcancel + vbExclamation, "Proceed to modify registry?") = vbok Then
		msgbox "No changes were made.", vbOkonly + vbInformation, "Cancelled at Operator Request"
		wscript.quit
	End If
End Sub

	
Sub subStatus(sStep, sComment)
	sStatus =  sStatus  & left(sStep & string(15," "), 15) & vbTab & sComment & vbCrlf 
End Sub