Automated USB wim deployment

For a customer I was asked to create a easy way to deploy  images to thin clients and thin pc’s. The preference was to use a USB stick as network was not a option.

This brought a number of challenges that I will discuss in this blog.

Fase 1 -  Create the WinPE Usb stick
Fase 2 – Add the needed packages to the USB stick
Fase 3 – Get and prepare the WIM file
Fase 4 – Give WinPE Menu options to choose from
Fase 5 – Deploying the WIM
Fase 6 – Renaming the deployed computer
Fase 7 -  Recreate the BCD
Fase 8 – Re enable the write filter

Before we can start, we need a bootable USB stick with WinPE.
To create this we need a deployment server preferable with Windows server 2012.

Fase 1
image
Plug in the USB stick and open the
“Deployment and Imaging Tools Environment”

There are 3 options for the PE environment
X86, amd64 or arm. For this blog I will go for the X86 one.

 

 

First you need to create the right files to make the WinPE environment, so give the following command

copype X86 C:\WinPE_X86”

image

When this is done the next step is to get WinPe on the USB stick.
This is done by the following command:
MakeWinPEMedia /UFD C:\WinPE_x86 F:
F: being the USB drive.

image
Ok, fase 1 complete!

Now let’s proceed to the next fase!

Fase 2

Because we going to fill the “startnet.cmd” file further on we going to need some extra packages in this PE build. (All the packages we will need are on the deployment server by default in “C:\Program Files <x86>\Windows Kits\8.0\Assessment and Deployment Kit\Windows Preinstallation Environment\x86\WinPe_OCs\”)

First you want to add the WMI package, therefor u will need to mount the image.
So open up a elevated rights cmd screen and give the following command:

dism /mount-wim /wimfile:F:\soirces\boot.wim /index:1 /mountdir:C:\Mounted_images\

image

Ok now lets add the WMI package

dism /image:c:\mounted_images\ /add-package /packagepath:”C:\Program Files <x86>\Windows Kits\8.0\Assessment and Deployment Kit\Windows Preinstallation Environment\x86\WinPe_OCs\WinPE-WMI.cab”

image
Next you will need the Scripting package:

dism /image:c:\mounted_images\ /add-package /packagepath:”C:\Program Files <x86>\Windows Kits\8.0\Assessment and Deployment Kit\Windows Preinstallation Environment\x86\WinPe_OCs\WinPE-Scripting.cab”

image

Perform the same steps for the MDAC and HTA  packages

dism /image:c:\mounted_images\ /add-package /packagepath:”C:\Program Files <x86>\Windows Kits\8.0\Assessment and Deployment Kit\Windows Preinstallation Environment\x86\WinPe_OCs\WinPE-MDAC.cab”

dism /image:c:\mounted_images\ /add-package /packagepath:”C:\Program Files <x86>\Windows Kits\8.0\Assessment and Deployment Kit\Windows Preinstallation Environment\x86\WinPe_OCs\WinPE-HTA.cab”
image

Ok, now that we have all the needed packages added we can start to dismount the image.
dism /Unmount-Wim /Mountdir:c:\mounted_images\ /commit

image

Take the USB drive and leave the server alone Winking smile.

Fase 2 Complete!

Fase 3

So far it has been pretty straight forward.

Before you continue with the USB drive it is time to get the WIM files we want to deploy.
To create these WIM files you going to need 2 tools. (Put them on the USB drive)
Weapons of choice:

EasyBCD, a very easy tool to edit the BCD – https://neosmart.net/EasyBCD/
ImageX tool to create WIM images – http://technet.microsoft.com/en-us/library/cc722145%28v=ws.10%29.aspximage

Boot the computer of which you want a WIM file. First you will need to install easyBCD to edit the BCD (Boot Configuration Data)

image

The next step might sound a bit strange but is really needed to prevent problems further on. Delete all the entry’s that are on the computer

Choose “Edit boot menu” select the entry and delete.

Click “Save settings” and exit. EasyBCD will give you a error telling you the computer wont boot without a record, ignore that.

Before you are going to create the WIM file, make sure to deinstall easyBCD.
Main reason for this is that you don’t want to deploy easyBCD on every computer because it’s in the image Winking smile.

Ok enough talk, lets make the WIM file.

Open up a cmd and navigate to the imagex directory.
Now enter:

Imagex /capture c: d:\thinpc\Tpc.wim “Thin PC”
Where D: is the USB drive.
image

Ok Fase 3 completed! we are Flying!

Fase 4

Now for the fun stuff.

Connect the USB drive to your own computer and mount the image (Remember Fase 2)
dism /mount-wim /wimfile:F:\soirces\boot.wim /index:1 /mountdir:C:\Mounted_images\

Once this is done navigate to the mounted image in “C:\mounted_images” and copy startnet.cmd from the system32 folder to your local machine.

image

 

When you open the file you will notice there is only 1 line in there with wpeinit. Never delete that line!

 

 

As I’m a big fan of “Idiot-proof” deployment I suggest u make a selectable menu when booting from USB, with every choice installing a different WIM.

In my situation it is needed there is a deployment for a Thin PC and a Thin Client.
So I will use that in this blog. However u can of course fill in your own WIM files that u need.

To create a menu choice:

wpeinit
@ECHO OFF
C:
CD\
CLS

:MENU

ECHO ============= Installeren Thin Image =============
ECHO ————————————————–
ECHO 1.  Thinclient T5740
ECHO ————————————————–
ECHO 2.  ThinPC 8200
ECHO ————————————————–
ECHO ==========PRESS ‘Q’ TO QUIT==========
ECHO.

SET INPUT=
SET /P INPUT=Please select a number:

IF /I ‘%INPUT%’==’1’ GOTO Selection1
IF /I ‘%INPUT%’==’2’ GOTO Selection2
IF /I ‘%INPUT%’==’Q’ GOTO Quit
CLS

ECHO ============INVALID INPUT============
ECHO ————————————-
ECHO Please select a number from the Main
echo Menu [1-2] or select ‘Q’ to quit.
ECHO ————————————-
ECHO ======PRESS ANY KEY TO CONTINUE======

PAUSE > NUL
GOTO MENU
CLS
:Selection1

:Selection2

:Quit
CLS
ECHO —————————————————-
ECHO =============PRESS ANY KEY TO CONTINUE==============
ECHO —————————————————-

PAUSE>NUL
EXIT

 

Ok, the menu is there but what does it do!?

Lets focus on Selection 1 (if someone enters 1 as there choice)

When someone chooses 1, I want imagex to install the wim on the c drive.
Easier said then done because the WIM is on the USB drive and the USB drive letter is variable..

After searching Google a bit I found a easy command which gives us the USB drive!

for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do (

if %%l equ 2 (
echo %%i is a USB drive.
        )

Ok but now it still needs to install the WIM, so lets add a line.

cd\
%%i\Imagex.exe /apply %%i\Thinclient\TC.WIM 1 C:
        )

%%I will be filled in by the USB drive letter thanks to the  first command
The “\Thinclient\TC.Wim” is nothing more then the wim location.
Ok now when choosing option “1” it will start to install the right WIM.

Selection 1 should now look something like:

image
Finishe…. No not yet! But fase 4 complete!

Fase 5

Before installing the WIM we want de disk to be clean of course, so lets build in a little disk part to make sure its installing on a clean disk.

First make a folder on the USB drive called “Diskpart” in that folder make a text file called “Diskpart.txt” put the following lines in the txt file.

Select Disk 0
Clean
Create part prim
Select part 2
Format fs=ntfs quick label=Windows
Assign letter=C
Active
Exit

Ok, save and exit.

Now remember selection 1 from fase 4? Add this line in there,

echo %%i is a USB drive.
        )
Diskpart /s %%i\diskpart\diskpart.txt
cd\
%%i\Imagex.exe /apply %%i\Thinclient\TC.WIM 1 C:
        )

This way diskpart wil run the commands right before im
agex puts the image back.

Ok we are almost done!

Fase 6

Next up is the computer name, u don’t want every computer having the same hostname.
To achieve this I made use of a VBS (Visual Basic) script which I found on the following site: Here

Safe it under rename.vbs

Unfortunately this is not enough, if we run this script from PE it will get the hostname PE uses. So let’s make sure the script is run when windows is up and running.

First put the script in a folder on the USB stick and note down the location.
Also make a simple bat file with the following commands in them:

———————
C:\rename.vbs
del rename.vbs
———————

Safe the bat in the diskpart folder on the USB drive (I called it diskid.bat)
Now add the following line under the ImageX command:

Copy D:\Location of the VBS file "c:\”
Copy D:\diskpart\diskid.bat "c:\users\user\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"

Ok this will take care of the renaming of the computer.

Next up Fase 7

Somewhere in Fase 3 we have deleted the BCD entries from the WIM image we are putting back now. Lets make sure it has a correct and updated entry when its finished installing the wim.

This is very easy to do but also very important.

Right on the end of the startnet.cmd file there is a line “:quit”
We will be entering the command right after this line.

Basicly its nothing more then “BCDBoot c:\windows /s c:”

:Quit
CLS
BCDBOOT C:\Windows /s c:

This will insert a correct BCD record in the image.

If you don’t have write filter enabled on your images u can now save the starnet.cmd to “c:\mounted_images\windows\system32” folder and unmount the image by opening dos and entering “dism /Unmount-Wim /Mountdir:c:\mounted_images\ /commit”

The usb is now bootable and will deploy the image automatically when pressing 1 in the menu.

If you do have Write filter enabled in your image keep reading for fase 8!

Usually the embedded software has EWF enabled.

One thing EWF does to work properly is set the hard drive identity in the registry.
So after you put back the WIM the software will have the wrong Drive id in its registry.

Lets solve this!

First we need the current drive id, the easiest way to get this is trough disk part.

diskpart –> select the correct disk – uniqueid disk.

image

But this isn’t called “Automated USB wim deployment” for nothing, so lets automate.
First open up the USB drive and navigate to the diskpart folder.
In here create a new text file called diskpartID.txt

In this file enter the following lines:

image

Now go back to the startnet.cmd file and enter the following line just below imagex:
Diskpart /s D:\diskpart\diskpartID.txt > C:\diskid.txt 2>&1
image

(2>%1 makes sure it will do the export even though the output is standard)

Ok, now you have exported the disk id to a text file called diskid.txt on the c drive.
But you don’t want the entire output u just want the number.

In fase 6 we made a bat file that will run once windows boots so lets abuse this file for the part also.

Add the following lines to the bat file:

for /f "tokens=3" %%a in (‘findstr "Disk ID:" c:\diskid.txt’) do set Diskid=%%a
(This will take only the disk id from the diskpart export u made)

reg delete HKLM\SYSTEM\CurrentControlSet\services\Ewf\Parameters\Protected\Volume0\ /v DiskSignature /f
(This will delete the old disk id value from the registry)

reg add HKLM\SYSTEM\CurrentControlSet\services\Ewf\Parameters\Protected\Volume0\ /v DiskSignature /t REG_DWORD /d 0x%diskid%
(This will add the new disk id to the registry)

Copy C:\diskid2.bat "c:\users\user\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
(Diskid2 will be created later on)

c:\deldiskid.bat
(deldiskid will be created later on

C:\rename.vbs
del c:\rename.vbs
(This lines were in the bat originally)

The batch should look something like this:
image

Ok first step is made.
before you continue lets cleanup first.
Create a new file called deldiskid.bat in diskpart folder of the USB stick

In this file enter the following:

del "c:\users\user\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\diskid.bat"
(This will delete the batch so it wont run every time we boot)
shutdown -r

Second step made!

In the first file we also made a reference to a file called c:\diskid2.bat

Same like before create this file in the diskpart folder of the USB drive and call it diskid2.bat
In this file add the following:

ewfmgr c: –enable
(This will enable the write filter)
ewfmgr c: –commit
(This will commit the current image to the write filter)
c:\deldiskid2.bat
(this will run deldiskid2.bat)

image

Last step!

Create one more file called deldiskid2.bat in the same folder like the rest.
In this file enter the following:

del "c:\users\user\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\diskid2.bat"
del c:\deldiskid.bat
del c:\PCrename.vbs
shutdown -r

This will cleanup the last files
Now open up the startnet.cmd and enter these last command
s:

%%i\Imagex.exe /apply %%i\ThinPC\TPC.WIM 1 C:
Diskpart /s D:\diskpart\diskpartID.txt > C:\diskid.txt 2>&1
Copy D:\diskpart\diskid.bat "c:\users\user\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
Copy D:\Location of the VBS file "c:\”
***(above is already in there)***

Copy D:\diskpart\diskid2.bat c:\
Copy D:\diskpart\deldiskid2.bat c:\
Copy D:\diskpart\deldiskid.bat c:\

Finished!

The startnet in total should look something like this:  (I had 2 images on the stick so that’s why there 2 inputs below)

————————————————————————
wpeinit
@ECHO OFF
C:
CD\
CLS

:MENU

ECHO ============= Installeren Thin Image =============
ECHO ————————————————–
ECHO 1.  Thinclient T5740
ECHO ————————————————–
ECHO 2.  ThinPC 8200
ECHO ————————————————–
ECHO ==========PRESS ‘Q’ TO QUIT==========
ECHO.

SET INPUT=
SET /P INPUT=Please select a number:

IF /I ‘%INPUT%’==’1’ GOTO Selection1
IF /I ‘%INPUT%’==’2’ GOTO Selection2
IF /I ‘%INPUT%’==’Q’ GOTO Quit
CLS

ECHO ============INVALID INPUT============
ECHO ————————————-
ECHO Please select a number from the Main
echo Menu [1-2] or select ‘Q’ to quit.
ECHO ————————————-
ECHO ======PRESS ANY KEY TO CONTINUE======

PAUSE > NUL
GOTO MENU
CLS
:Selection1

for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do (

if %%l equ 2 (
echo %%i is a USB drive.
        )
Diskpart /s %%i\diskpart\diskpart.txt
cd\
%%i\Imagex.exe /apply %%i\Thinclient\TC.WIM 1 C:
Diskpart /s D:\diskpart\diskpartID.txt > C:\diskid.txt 2>&1
Copy D:\diskpart\diskid.bat "c:\users\user\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
Copy D:\diskpart\diskid2.bat c:\
Copy D:\diskpart\deldiskid2.bat c:\
Copy D:\diskpart\deldiskid.bat c:\
Copy D:\ThinPC\PCrename.vbs c:\
        )

GOTO Quit

:Selection2

for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do (

if %%l equ 2 (
echo %%i is a USB drive.
        )
Diskpart /s %%i\diskpart\diskpart.txt
cd\
%%i\Imagex.exe /apply %%i\ThinPC\TPC.WIM 1 C:
Diskpart /s D:\diskpart\diskpartID.txt > C:\diskid.txt 2>&1
Copy D:\diskpart\diskid.bat "c:\users\user\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
Copy D:\diskpart\diskid2.bat c:\
Copy D:\diskpart\deldiskid2.bat c:\
Copy D:\diskpart\deldiskid.bat c:\
Copy D:\ThinPC\PCrename.vbs c:\
        )

GOTO Quit

:Quit
CLS

C:
CD Windows
CD System32
BCDBOOT C:\Windows /s c:
CLS

ECHO ==============WOOP WOOP VICTORIOUS!!!===============
ECHO —————————————————-
ECHO —————————————————-
ECHO =============PRESS ANY KEY TO CONTINUE==============

PAUSE>NUL

EXIT

———————————————

Now u are finished!

You can now save the starnet.cmd to “c:\mounted_images\windows\system32” folder and unmount the image by opening dos and entering “dism /Unmount-Wim /Mountdir:c:\mounted_images\ /commit”

The usb is now bootable and will deploy the image automatically when pressing 1 in the menu.

An experienced and creative IT specialist that proactively contributes to achieving customers’ goals. Is highly driven to finish a job within the agreed time-frame. Special roles: systems engineer, system manager, project co-worker, team leader, migration specialist.

Core qualities
Customer-focused, energetic, collegial, critical, driven

Hobbies
Snowboarding, wakeboarding, jiu-jitsu, mountain biking, travelling

Job description
Systems Engineer