'----------------------------------------------------------------------------
' Script to update RM Proxy ADM template in Inf directory and in the Internet
' & Email GPO.
' GM 31/8/05
'----------------------------------------------------------------------------


Dim oShell, oFSO, sScriptDirectory, sAdmFileSource, sPolicyDisplayNames(9), sAdmPath, sInfAdmPath, sAdmFolderPath, sAdmDisplayName, iLength 
Dim re, iReturn, sDomain, TM_ADM_Folder_Path, TM_ADM_Folder, TM_IntEm_Adm_List, sInfAdm
set oShell = wscript.createObject("wscript.shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")


On Error resume next
'Option Explicit




' set up logging and log first message
sLogDirectory = oShell.RegRead("HKLM\Software\Research Machines\LogSettings\ComponentLogPath")
sLogfile = sLogDirectory & "\RM Tutor 4 ADM template script.log"

sMsg = "RM Tutor 4 ADM template script - starting" 
LogMsg sMsg, sLogfile, True


' Get the path to the where the script is running from. 
' ADM Templates will be in the same directory as this.

sScriptDirectory = oFSO.GetAbsolutePathName(".")

iLength = Len(sScriptDirectory)
If Right(sScriptDirectory, 1) = "\" Then
	sScriptDirectory = Left(sScriptDirectory, iLength - 1)
End If

sMsg = "Installing from " & sScriptDirectory
LogMsg sMsg, sLogfile, True

'set source file to be the new adm file
sAdmFileSource = sScriptDirectory & "\RM Tutor 4 Policies.adm"
sAdmPath = "\adm\RM Tutor 4 Policies.adm"

sPolicyDisplayNames(0) = "Guest UserType"
sPolicyDisplayNames(1) = "RM Explorer UserType"
sPolicyDisplayNames(2) = "Staff UserType"
sPolicyDisplayNames(3) = "Standard UserType"
sPolicyDisplayNames(4) = "System Administrator UserType"
sPolicyDisplayNames(5) = "CyberCafe StationType"
sPolicyDisplayNames(6) = "Personal StationType"
sPolicyDisplayNames(7) = "Shared Desktop StationType"
sPolicyDisplayNames(8) = "Shared Laptop StationType"
sPolicyDisplayNames(9) = "Terminal Server StationType"

sInfAdmPath = "\inf\Research Machines\RM Tutor 4 Policies.adm"
sAdmFolderPath = sScriptDirectory & "\RM Tutor 4 Policies.adm"
sAdmDisplayName = "\RM Tutor 4 Policies.adm"


'Connect to domain


sMsg = "Connecting to domain"
LogMsg sMsg, sLogfile, True



Dim oDSE, oPolicies, oPolicy, sPolFile, sFile
Set oDSE = GetObject("LDAP://RootDSE")
sDomain = oDSE.Get("defaultNamingContext")


'Connect to policy container (where GPOs are stored in AD)

sMsg = "Locating policies container"
LogMsg sMsg, sLogfile, True

Set oPolicies = GetObject("LDAP://CN=Policies,CN=System," & sDomain)

for iPolicyLoopCount = 0 to 9

	'Copy ADM to Policy Object

	' Find the Internet & Email GPO
	sMsg = "Searching for "
	sMsg = sMsg & sPolicyDisplayNames(iPolicyLoopCount)
	sMsg = sMsg & " GPO"
	
	LogMsg sMsg, sLogfile, True



	For Each oPolicy In oPolicies                       
		If oPolicy.DisplayName = sPolicyDisplayNames(iPolicyLoopCount) Then

			'Need to find path to GPO ADM folder
			sAdmFile = oPolicy.gPCFileSysPath & sAdmPath

			'Log message to say we are copying file
			sMsg = "Copying " & sAdmFileSource & " to " & sAdmFile
			LogMsg sMsg, sLogfile, True

			' copy new file
			oFSO.CopyFile sAdmFileSource, sAdmFile

			'set to read-only,archive,hidden
			set f = OFSO.GetFile(sAdmFile)
			f.attributes = 35		

			If Err.Number then
				
				Dim sMessage
				sMessage = "Error copying adm template to " & sPolicyDisplayNames(iPolicyLoopCount) & " GPO"
				Err.Clear
			End If

		End If
	 
	Next
Next



' Add the adm template in C:\WinNT\Inf\Research Machines

'Get windows directory path to allow for 2000 and 2003 machines
sInfAdm = oShell.ExpandEnvironmentStrings("%WINDIR%")

sInfAdm = sInfAdm & sInfAdmPath

sMsg = "Copying " & sAdmFolderPath & " to " & sInfAdm
LogMsg sMsg, sLogfile, True

'copy file to windows\Inf\Research machines directory
oFSO.CopyFile sAdmFolderPath, sInfAdm

If Err.Number then
	LogError("Error copying adm template to Inf directory")
End If

'**************************************
'Add template to type manager
'**************************************


'Find path for typemanagers adm files and add new adm
'*****************************************

TM_ADM_Folder_Path = oShell.RegRead("HKLM\Software\Research Machines\Network Management\Type Manager\ADM Folder")

'If typemanager is present then add relevant entries in registry and filesystem 
If Err.Number then
	LogError("Type manager not Present")
	
Else
	TM_ADM_Folder_Path = TM_ADM_Folder_Path & sAdmDisplayName

	sMsg = "Copying " & sAdmFolderPath & " to " & TM_ADM_Folder_Path
	LogMsg sMsg, sLogfile, True
	
	'Copy my ADM to this location
	oFSO.copyFile sAdmFolderPath, TM_ADM_Folder_Path

	'Add my ADM template file name to list in Type manager Reg Key
	TM_IntEm_Adm_List = oShell.RegRead("HKLM\Software\Research Machines\Network Management\Type Manager\UserType ADMs")

	re = "Tutor 4"
	
	'Check ADM is not already listed or Type manager will get confused
	if InStr(TM_IntEM_Adm_List, re) > 0 then
		sMsg = "Type manager user registry key already present" 
		LogMsg sMsg, sLogfile, True
	Else
		TM_IntEm_Adm_List = TM_IntEm_Adm_List & ";RM Tutor 4 Policies.adm"

		sMsg = "Adding Tutor ADM to Type Manager" 
		LogMsg sMsg, sLogfile, True

		oShell.RegWrite "HKLM\Software\Research Machines\Network Management\Type Manager\UserType ADMs", 	TM_IntEm_Adm_List, "REG_SZ"
	End If
	
	TM_IntEm_Adm_List = oShell.RegRead("HKLM\Software\Research Machines\Network Management\Type Manager\StationType ADMs")

	re = "Tutor 4"
	
	'Check ADM is not already listed or Type manager will get confused
	if InStr(TM_IntEM_Adm_List, re) > 0 then
		sMsg = "Type manager station registry Key already present" 
		LogMsg sMsg, sLogfile, True
	Else
		TM_IntEm_Adm_List = TM_IntEm_Adm_List & ";RM Tutor 4 Policies.adm"

		sMsg = "Adding Tutor ADM to Type Manager" 
		LogMsg sMsg, sLogfile, True

		oShell.RegWrite "HKLM\Software\Research Machines\Network Management\Type Manager\StationType ADMs", 	TM_IntEm_Adm_List, "REG_SZ"
	End If

	'End if Err.Number
End If

sMsg = "RM Tutor 4 ADM template script - finishing"
LogMsg sMsg, sLogfile, True


wscript.quit



'---------------------------------------------
' Write a message to a log file
'---------------------------------------------
Private Function LogMsg(sMsg, sFile, bDate)

	On Error Resume Next

	Dim a

  	Set a = oFSO.OpenTextFile(sFile, 8, True)
  	If bDate Then
    	a.WriteLine (Date & " " & Time & " - " & sMsg)
  	Else
    	a.WriteLine (sMsg)
  	End If
  
  	a.Close

End Function


'---------------------------------------------
' Error logging
'---------------------------------------------
Private Function LogError(sInsert)

	'Creates a message box if required
  	'Call Wscript.Echo( "Error 0x" & CStr(Err.Number) & " " & Err.Description )
	
	'Log Error in the Log file
	sMsg = sInsert & ": Error 0x" & CStr(Err.Number) & " Description: " & Err.Description & vbCr
  	LogMsg sMsg, sLogFile, True
  	
	Err.Clear
		
End Function


