| |
CondoPC
Posts: 118
Score: 0
Joined: 7/23/2007
Status: offline
|
For some reason the first attempt at posting this script caused the post to currupt. Here goes a second try. I am not using the code blocks because it corrupted the last time. I don't know why the forum breaks when large code lines are inserted, but here is a repost since I can only see 1/2 the code I posted in the other one. I was wrestling with some tweaks that we had to apply before and after SP2 gets installed on a server and thought I would share my efforts. This resolves the common issue where the catalog is corrupt so SP2 (or SP1) fails to install. Script will lokk for "Server 2003" and then validate that service pack level is under "2". I also have a tweak to get or remote DCOM applications to work by adding the Authenticated users to Distributed COM Users group. Script will perform the reboot if no errors occured. ' ****************************************************** ' ' Windows 2003 SP2 Installation ' '******************************************************* 'Author - Jason Condo ' 'Adapted from WISE installation script 'This will install SP2 on a Windows 2003 server and make the needed configuration changes ' 'Declare Script Environment 'On Error Resume Next '******************************************************* 'User defined Variables '******************************************************* stri386 = "e:\Tools-Software\i386" strSlipStreamPath = "e:\Tools-Software" strComputer = "." '******************************************************* 'Script Variables '******************************************************* Set objFSO = CreateObject("Scripting.FileSystemObject") Set objShell = CreateObject("WScript.Shell") objCurrDir = objShell.CurrentDirectory strScriptFile = WScript.ScriptFullname strScriptPath = Left(strScriptFile, Len(strScriptFile) - Len(WScript.Scriptname)) bUpgradeOK = false '******************************************************* '********** Main Script ******************************** '******************************************************* 'verify OS strOSVersion = GetOSver(strComputer) arrInfo = split(strOSVersion, ";") strOSName = arrInfo(0) iSPNumber = CInt(arrInfo(1)) If Instr(strOSName, "Server 2003") <> 0 Then If iSPNumber < 2 Then bUpgradeOK = true Else strMessage = strMessage & "-- Service Pack level does not meet requirements. Current install is Service Pack " & iSPNumber & Chr(10) End If Else strMessage = strMessage & "-- Operating System does not meet requirements. Current OS is " & strOSName & Chr(10) End If If NOT bUpgradeOK Then strMessage = "This server does not meet the requirements for installing Service Pack 2. " & Chr(10) & "The following errors occurred: " & Chr(10) & strMessage Call DisplayError(strMessage) wscript.Quit Else '******************************************************** 'perform upgrade '******************************************************* 'Clean up the CATROOT2 to prevent problems '-------------------------------------------------------- '1. Stop the Cryptographic Service service strSvcName = "CryptSvc" sQry = "Select * from Win32_Service where Name='" & strSvcName & "'" sDQry = "Associators of {Win32_Service.Name='" & strSvcName & "'} Where AssocClass=Win32_DependentService " & "Role=Dependent" sAQry = "Associators of {Win32_Service.Name='" & strSvcName & "'} Where AssocClass=Win32_DependentService " & "Role=Antecedent" Call ControlService(strComputer, strSvcName, "stop", sAQry) 'stop services that depend on this service Call ControlService(strComputer, strSvcName, "stop", sQry) 'stop the service '2. Delete/rename the "C:\Windows\System32\CatRoot2" folder (not the CatRoot folder) If objFSO.FolderExists("C:\Windows\System32\CatRoot2") Then msgbox "found CatRoot2" objFSO.DeleteFolder "C:\Windows\System32\CatRoot2",true msgbox "deleted" End If '3. Delete any "tmp*.cat" and "sp2.cat" files in the "C:\Windows\System32\CatRoot\{f*}" folder Dim strCatFldr : strCatFldr = "C:\WINDOWS\system32\CatRoot\{F750E6C3-38EE-11D1-85E5-00C04FC295EE}" If objFSO.FolderExists(strCatFldr) Then Set colFiles = objFSO.GetFolder(strCatFldr).Files For each oFile in colFiles Dim strFileName : strFileName = lcase(oFile.Name) dim strExt : strExt = objFSO.GetExtensionName(oFile.Name) 'get extension If lcase(strExt) = "cat" Then If left(strFileName, 3) = "tmp" OR strFileName = "sp2.cat" Then oFile.Delete true DisplayOutput "Deleted " & strFileName End If End If Next End If '4. Restart the Cryptographic Service service, if it hasn't already restarted. Call ControlService(strComputer, strSvcName, "start", sQry) 'start service Call ControlService(strComputer, strSvcName, "start", sDQry) 'start services that depended on this service 'slipstream SP2 '-------------------------------------------------------- If objFSO.FolderExists(stri386) Then 'find i386 DisplayOutput "Please wait while we slipstream SP2 into the " & stri386 & " directory." strCommand = strScriptPath & "\i386\update\update.exe /passive /norestart /n /integrate:" & strSlipStreamPath ObjShell.Run strCommand, ,true End If err.Clear 'install SP2 '-------------------------------------------------------- DisplayOutput "Updating Windows ..." strCommand = strScriptPath & "\i386\update\update.exe /passive /norestart /n" 'no backup and no restart ObjShell.Run strCommand, ,true If err.number <> 0 Then DisplayError "An error has occurred during the patch process. Press [ OK ] to open the logfile" & Chr(10) & err.Description & Chr(10) & err.number Wscript.CreateObject("WScript.Shell").Run "C:\Windows\svcpack.log" wscript.Quit End If '******************************************************* ' apply fixes '******************************************************* 'add users to local DCOM group '-------------------------------------------------------- Call AddToLocalGroup("Distributed COM Users","NT AUTHORITY\ANONYMOUS LOGON") Call AddToLocalGroup("Distributed COM Users","NT AUTHORITY\Authenticated Users") 'If webserver, set folder secuity for anonymous access for the Navigator Folder in website '-------------------------------------------------------- 'Reboot Server '-------------------------------------------------------- Call ServerReboot(strComputer) End If wscript.Quit '******************************************************* '****** Script Functions and Subs ***************** '******************************************************* Function GetOSver(strComputer) Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colOperatingSystems = objWMIService.ExecQuery _ ("Select * from Win32_OperatingSystem") For Each objOperatingSystem in colOperatingSystems If objOperatingSystem.Version <> "" then GetOSver = objOperatingSystem.Caption & ";" & objOperatingSystem.ServicePackMajorVersion End If Next End Function '----------------------------------- Sub AddToLocalGroup(strLGroup,strMember) strUsrTool = strScriptPath & "\Tools\CUSRMGR.EXE" 'verify user management tool exists If OjbFSO.FileExists(strUsrTool) Then 'adds a member (strMember) to the local group (strLGroup) strCommand = strUsrTool & " -u " & strMember & " -dlg " & strLGroup objShell.Run strCommand, 0, true End If End Sub Sub DisplayOutput(strText) wscript.echo strText End Sub Sub DisplayError(strText) msgbox strText, 16, "Error" End Sub '=== Used to control a service ==== Function ControlService(strComputer, strSvcName, strProcess, strSQL) 'On Error Resume Next 'this start service and dependants Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colServiceList = objWMIService.ExecQuery(strSQL) DIM iQryResult, iResult For each objService in colServiceList iResult = "" Select Case lcase(strProcess) Case "start" DisplayOutput "." & vbCr & "-- Please wait while the " & objService.Name & " is " & strProcess & "ed" objService.StartService() iQryResult = 0 Case "stop" DisplayOutput "." & vbCr & "-- Please wait while the " & objService.Name & " is " & strProcess & "ped" objService.StopService() iQryResult = 6 End Select 'DisplayOutputStatic " looking for " & iQryResult DIM sP : sP = ".." Do Until iResult = iQryResult '0:Success - 6:Service Not Active iResult=objService.InterrogateService() 'DisplayOutputStatic sP ScriptSleep 2000 Loop DisplayOutput "-- The " & strProcess & " command has completed on the " & objService.Name & " service" Next objWMIService = NULL colServiceList = NULL objService = NULL End Function '----------------------------------------- Sub ScriptSleep(MSec) wscript.Sleep MSec End Sub '----------------------------------------- Function ServerReboot(strComputer) Err.Clear 'Comment and reason code for the shutdown dim strCommand : strCommand = Chr(34) & "***Service Pack Installtion***" & Chr(34) & " /f /d p:4:1" Set objWMIService = GetObject("winmgmts:{(Shutdown)}!//" & strComputer & "/root/cimv2:Win32_Process") If Err.number <> 0 Then DisplayError "Error rebooting the server" Else 'Execute shutdown command with arguments ServerReboot = objWMIService.Create("C:\Windows\system32\shutdown.exe /r /t 30 /c " & strCommand) End If End Function again, thanks to ehvbs and dm_4ever for helping with the CatRoot cleanup
|
|