The Build Disc – Part 2 (Service Packs)

Windows Service PackIn part 1 I offered an overview of my goals for creating the build disc we use here.  In part 2, I’m taking a more detailed look at service packs and how this got automated and streamlined.

My goals for handling service packs were:

  • Identify the operating system.
  • Identify the current service pack level.
  • Decide if a service pack is needed.
  • If needed, go ahead and install the service pack without any further user interaction.

Identify the operating system:

While there’s plenty of choices out there, I’ve decided that I really only need to worry about Windows XP and Windows Vista for the time being.  Our business doesn’t handle enough servers to warranty including these on the disc.  This simplified OS identification down to an either/or choice.

Windows XP will report “Microsoft Windows XP [Version 5.1.2600]” while Vista reports “Microsoft Windows [Version 6.0.6001]“.

Taking this information we can now work out an operating system:

ver | find /i "Windows XP" >NUL
if %ERRORLEVEL% equ 0 (
  echo Found: Windows XP
)
ver | find /i "Version 6" >NUL
if %ERRORLEVEL% equ 0 (
  echo Found: Windows Vista
)

Identify Service Pack Level:

This time we’re going with ‘SystemInfo’ to get the information we need.  The command actually provides a considerable amount of information but, among that information, is the OS Version including service pack level.

For Vista this lets us do something like this:

ver | find /i "Version 6" >NUL
if %ERRORLEVEL% equ 0 (
  echo Found: Windows Vista
  echo  - Checking for Service Pack Level...
  set OSVER=5
 
  SystemInfo | FIND /I "Service Pack 1" >NUL
  IF NOT ERRORLEVEL 1 (
    echo Found: Service Pack 1 - no update required.
    goto End
  )
 
  echo No Service Pack Installed - installing Service Pack 1
)
:End

with a similar approach for Windows XP (taking into account three service packs).  This step also determines if a service pack needs to be installed.  By using a “goto End” for the current service pack and leaving all the rest to go through the batch file you can skip over service pack installation if the current service pack is already installed.

Silent Install:

If you run up a command prompt and type “servicepack.exe /?” you’ll get a short description of command switches.  This helps work out what you need to do a silent install.

For Windows XP’s service pack 3 I’m using:

start /wait .ServicePacksWinXP-SP3.exe /passive /warnrestart

and for Windows Vista’s service pack 1 I’m using:

start /wait .ServicePacksVista-SP1-KB936330-X86-wave0.exe /unattend /warnrestart

Putting it together:

Combining all these bits, plus adding some extra stuff to work out the drive letter for the DVD that the batch file is running from,  I end up with this batch file:

@ECHO OFF
echo **************************************
echo * Build DVD                          *
echo *                                    *
echo * Install Service Pack               *
echo **************************************
echo.
echo Checking drives for build disc...
rem ==========================================================
rem * When this batch file is launched from the gui prog it  *
rem * doesn't retain a path and, therefore, can't find any   *
rem * of the stuff it's supposed to install.  This routine   *
rem * is used to locate the DVD that contains the required   *
rem * information.                                           *
rem *                                                        *
rem * The batch file works in Vista but currently benefits   *
rem * from having UAC disabled.  Working on a future version *
rem * that will work with UAC being enabled.                 *
rem ==========================================================
SET CDROM=
SET Temp1=%Temp%.%~n0%Random%1.reg
SET Temp2=%Temp%.%~n0%Random%2.reg
START /WAIT REGEDIT /E %Temp1% "HKEY_LOCAL_MACHINESYSTEMMountedDevices"
TYPE %Temp1% > %Temp2%
TYPE %Temp2% | FIND "\DosDevices\" | FIND /V "\DosDevices\A:" | FIND "=hex:5c," > %Temp1%
FOR /F "tokens=3 delims=:" %%A IN (%Temp1%) DO CALL :ParseDrive %%A
DEL %Temp1%
DEL %Temp2%
SET Temp1=
SET Temp2=
echo Build Disc Found: %CDROM% drive.
cd /d %CDROM%:
GOTO:FindOSVer
:ParseDrive
IF EXIST %1:buildversion.txt (
  SET CDROM=%1
)
GOTO:EOF
:FindOSVer
rem ==========================================================
rem * This batch file supports Vista and XP - need to work   *
rem * out which one we're dealing with and see if it has the *
rem * latest service pack installed.                         *
rem * This will also start a silent install of the service   *
rem * pack as required.                                      *
rem ==========================================================
echo.
echo Checking Windows Version...
rem ==========================================================
rem * Test for Windows XP here...                            *
rem ==========================================================
ver | find /i "Windows XP" > nul
if %ERRORLEVEL% equ 0 (
  echo Found: Windows XP
  echo  - Checking for Service Pack Level...
  set OSVER=5
  SystemInfo | FIND /I "Service Pack 1" >NUL
  IF NOT ERRORLEVEL 1 (
    echo Found: Service Pack 1 - installing Service Pack 3
    start /wait .ServicePacksWinXP-SP3.exe /passive /warnrestart
    goto End
  )
  SystemInfo | FIND /I "Service Pack 2" >NUL
  IF NOT ERRORLEVEL 1 (
    echo Found: Service Pack 2 - installing Service Pack 3
    start /wait .ServicePacksWinXP-SP3.exe /passive /warnrestart
    goto End
  )
  SystemInfo | FIND /I "Service Pack 3" >NUL
  IF NOT ERRORLEVEL 1 (
    echo Found: Service Pack 3 - no update required.
    goto End
  )
  echo No Service Pack Installed - installing Service Pack 3
  start /wait .ServicePacksWinXP-SP3.exe /passive /warnrestart
  goto End
)
rem ==========================================================
rem * Test for Windows Vista here...                         *
rem ==========================================================
ver | find /i "Version 6" >nul
if %ERRORLEVEL% equ 0 (
  echo Found: Windows Vista
  echo  - Checking for Service Pack Level...
  set OSVER=5
  SystemInfo | FIND /I "Service Pack 1" >NUL
  IF NOT ERRORLEVEL 1 (
    echo Found: Service Pack 1 - no update required.
    goto End
  )
  echo No Service Pack Installed - installing Service Pack 1
  start /wait .ServicePacksVista-SP1-KB936330-X86-wave0.exe /unattend /warnrestart
  goto End
)
rem ==========================================================
rem * Failed to find valid operating system.                 *
rem ==========================================================
echo.
echo ERROR: Operating system not supported.
echo        (Windows XP and Windows Vista only)
pause
:End

That’s it for service packs.  Next up we’ll look at Windows Updates.