Little Shop of Drivers

I take all my drivers and put them in %DRIVERS_ROOT_PATH% (see batch code below) and the install images I want to mess with in %FILES_ROOT_PATH%.  One folder per driver, the script iterates through each of the folders, and runs imagex /inf for each folder.  As I'm testing, this makes it much easier to start over from scratch as I was trying to get different stuff to work.

Note:  I wrote this for the x64 Install.wim which only has 4 images in it, the x86 has 7, but it will work for x86 WIM if the bold/italicized number below is changed.  Then all the images within the WIM will be updated.

Note:  I have put some work into this since my initial posting, and determined updating, rather than reposting made the most sense.  This will now also automate adding packages to the image so long as the packages are in %PACKAGES_ROOT_PATH% (Ensure the Directory name is the same as the .CAB file from the package so that it knows which CAB to install).
This has also been generalized to work with a WIM that has any number of images.  This script assumes that the WIM file is in root of the folder structure that the drivers and packages are in, but that can easily be changed using the SET statements below.

@echo off
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::Check Inputs
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
IF "%1"=="" (
Echo Enter the directory root for the drivers and packages to add to the image.
GOTO END
)

IF "%2"=="" (
Echo Enter WIM file name.  This must be in the root of the
GOTO END
)

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::SET Variables
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
SET REFERENCENAME=%1
SET MOUNTPOINT=D:\FOO\%REFERENCENAME%
SET FILES_ROOT_PATH=D:\%REFERENCENAME%
SET IMAGEFILE=%FILES_ROOT_PATH%\%2
SET DRIVERS_ROOT_PATH=%FILES_ROOT_PATH%\Drivers
SET PACKAGES_ROOT_PATH=%FILES_ROOT_PATH%\Packages
SET LOGS_ROOT_PATH=%FILES_ROOT_PATH%\Logs
SET WIN_AIK_INSTALL_PATH=C:\Program Files\Windows AIK\Toolsecho %WIN_AIK_INSTALL_PATH%
Goto :End

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::Ensure needed directories exist and are ready to be used
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
if not exist %MOUNTPOINT% (md %MOUNTPOINT%) ELSE *"c:\Program Files\Windows AIK\Tools\x86\imagex.exe" /unmount %MOUNTPOINT%)
if not exist %LOGS_ROOT_PATH% (md %LOGS_ROOT_PATH%) ELSE (del /s /q %LOGS_ROOT_PATH%>NUL)

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::Identify number of images in WIM and process each image
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
for /f "tokens=1,2 delims=:" %%i in ('imagex /info %IMAGEFILE%') do if "%%i"=="Image Count" SET IMAGE_COUNT=%%j
Echo This WIM contains%IMAGE_COUNT% image(s).
For /l %%i in (1,1,%IMAGE_COUNT%) do call :update %IMAGEFILE% %%i
GOTO END

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::Process per image steps
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:update
Echo Updating %1 - Image #%2
"c:\Program Files\Windows AIK\Tools\x86\imagex.exe" /mountrw "%1" %2 %MOUNTPOINT%
for /f %%i in ('dir /ad /b %DRIVERS_ROOT_PATH%') do Call :InstallDriver %%i
for /f %%i in ('dir /ad /b %PACKAGES_ROOT_PATH%') do Call :InstallPackage %%i %2
"%WIN_AIK_INSTALL_PATH%\x86\imagex.exe" /unmount /commit %MOUNTPOINT%
goto :EOF

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::Install a specified driver
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:InstallDriver
Echo Installing Drivers from %DRIVERS_ROOT_PATH%\%1
"C:\Program Files\Windows AIK\Tools\PETools\peimg.exe" /verbose /inf=%DRIVERS_ROOT_PATH%\%1\*.inf /image=%MOUNTPOINT%>NUL
IF ERRORLEVEL 1 ECho       ERROR:  Couldn't Install Driver "%1"
goto :EOF

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::Install a specified package
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
f:InstallPackage
Echo Installing Package %PACKAGES_ROOT_PATH%\%1
"%WIN_AIK_INSTALL_PATH%\Servicing\pkgmgr" /n:"%PACKAGES_ROOT_PATH%\%1\%1.xml" /o:%MOUNTPOINT%;%MOUNTPOINT%\Windows /s:%TEMP% /l:%LOGS_ROOT_PATH%\%2-%1>NUL
IF ERRORLEVEL 1 ECho       ERROR:  Couldn't Install Package "%1"
:: /m:"%PACKAGES_ROOT_PATH%\%1\%1.cab"
GOTO :EOF

:END
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::Clean up variables
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
SET IMAGE_COUNT=
SET REFERENCENAME=
SET IMAGEFILE=
SET MOUNTPOINT=
SET FILES_ROOT_PATH=
SET DRIVERS_ROOT_PATH=
SET PACKAGES_ROOT_PATH=
SET WIN_AIK_INSTALL_PATH=