Bare Metal Restore Using iSCSI with Server 2012/2012R2

If you are using an iSCSI connection to backup your 2012/2012R2 servers you may also want to perform a bare metal restore using iSCSI. If you've ever done a bare metal restore using the Windows Server 2012/2012R2 media the standard process is to boot to the installation DVD matching the OS being restored, click repair, browse through a couple more choices, and finally choose 'System Image Recovery'. At this point you can choose a backup on a direct-attached disk or connect to a network share. When the backup is on an iSCSI device there are two basic ways to access it which can divided into the easy way and the hard way.

The easy way is to:
- attach the iSCSI LUN to server on your network if it is not already connected
- ensure a drive letter is assigned
- boot the target restore server from the installation DVD matching the OS to be restored
- perform the standard steps using 'System Image Recovery' to connect to the iSCSI backup as a network share
- perform the bare metal restore. Note that there's an option in 'System Image Recovery' to include additional storage drivers if needed to write to your server storage and also to prevent a reboot if you are cloning a server.

There are a number of reasons you may want to perform a bare metal restore from an iSCSI device directly. I consider this the hard way because of the following.

- Using the 2012/2012R2 installation DVD to enter the recovery WinRE environment does not natively provide the tools to attach directly to an iSCSI device. 
- Specifically, within the WinRE recovery environment the service 'Microsoft iSCSI Initiator Service' will not start without prestaging several additional WinRE DISM features and also configuring an active network connection. Lastly, an iSCSI GUI is not present to create the iSCSI connection which may be problematic if you're not familiar with iSCSI command line syntax.

If you're convinced you'd like to proceed the following approach works successfully.

At a high level the approach is to:

- Determine whether you would like to restore a 2012 or 2012 R2 OS. Retrieve the appropriate installation DVD or .iso.
- Since when clicking Repair from the installation DVD you are moving to different boot environment called WinRE its necessary to modify the WinRE environment by obtaining the WinRE.wim file. The WinRE.wim is located inside the install.wim on the installation DVD.
- Use dism to mount the install.wim and copy out the winre.wim so it can be modified. Also, copy the pertinent OS-specific iscsi files from the install.wim.
- Install the ADK matching the OS version to be restored. Copy the feature .cab files to a safe place.
- Mount the winre.wim
- Copy the OS-specific iSCSI files to the WinRE mount directory
- Add a batch file with the networking commands to the WinRE mount directory
- Use DISM to install the pertinent DISM features into the WinRE mount directory
- Commit the changes to the winre.wim
- Integrate the winre.wim into a bootable WinPE/RE .iso. I chose a MDT .iso I already had. Alternatively you could create a bootable .iso via a different method.
- Boot up to the created .iso or burn a CD/DVD
- Choose 'Command Line', ensure networking is working, setup the iSCSI connection with the iSCSI GUI, and then go back into 'System Image Recovery' and perform the bare metal restore.

At a more specific level it makes sense to first collect all the necessary resources and then run a script to integrate the components into the WinRE environment:

- Create the following directory structure to hold the resources you'll collect. This will line up with a batch file that can be used to update the winre.wim file.
  Main Directory
      2012Iscsifiles
Drivers
en-us

      2012r2Iscsifiles
Drivers
en-us
2012packages
en-us
2012r2packages
en-us

      netcmds
      Orig2012winre
      Orig2012r2winre
Updated2012winre
Updated2012r2winre
-
Copy the install.wim from the installation DVD matching the OS to be restored (2012 or 2012 R2)
- Create a temporary mount directory
- Run the dism command with admin rights to mount install.wim to obtain the winre.wim file: Dism /Mount-Image /ImageFile:"<path to install.wim>" /Index:1 /MountDir:"<path to mount dir>"
- Ensure file explorer is set to view operating system files since winre.wim is hidden. Copy "<path to mount dir>\windows\system32\recovery\winre.wim" to the predefined Orig<OS>winre and Updated<OS>winre directories.
- Copy the necessary iscsi files some of which may be redundant. Copy the files starting with iscsi* from the "<path to mount dir>\Windows\System32" directory to the predefined <OS>iscsifiles directory. Copy the file msiscsi.sys from the "<path to mount dir> \Windows\System32\Drivers" directory to the predefined <OS>iscsifiles\Drivers directory. Finally, copy the files starting with iscsi* from the "<path to mount dir>\Windows\System32\en-us" directory to the predefined <OS>iscsifiles\en-us directory.
- Create a batch file within the predefined netcmds directory named netdhcp.cmd. Edit the file by adding the following lines below, and save it in the predefined netcmds directory. Note the remarked out line for a static IP that can be used if necessary.
  netcfg -winpe
  wpeutil InitializeNetwork
  REM - enable for static - netsh int ipv4 set address "Ethernet" static <staticAddress> <subnet> <gateway>
  net start msiscsi
  iscsicpl
- Retrieve the DISM feature cab files from the appropriate version of the ADK depending on whether its 2012 or 2012 R2. If you don't have the appropriate version available and installed you'll need to download and install it first. The 2012 version of the ADK can be downloaded from: https://www.microsoft.com/en-us/download/details.aspx?id=30652 The 2012 R2 (with update) version of the ADK can be downloaded from: https://www.microsoft.com/en-US/download/details.aspx?id=39982 Once the appropriate ADK is installed, copy the contents of the ADK directory "c:\program files (x86)\Windows Kits\8.x\Assessment and Deployment Kits\Windows Preinstallation Environment\amd64\WinPE_OCs" and its en-us subdirectory to the predefined <OS>packages directory.
- The following batch files can be used to update the winre.wim file with the collected resources. Create of copy of each batch file in the main predefined directory which allows the relative paths to work correctly. To run, right-click on the batch file for the right OS and choose 'Run as administrator'. Note that there are separate batch files for 2012 and 2012 R2 that have slight differences. The pause statements are simply to allow you to stop the process if you notice an error. Note that if you need to make successive runs with the batch file you'll want to copy the original winre.wim from the predefined Orig<OS>winre directory to the predefined Updated<OS>winre directory to ensure you start fresh with each run.

2012 Batch File contents: Rem For 2012
rem mount winre from 2012 media (that's inside install.wim)

md c:\temp\mount
Dism /Mount-Image /ImageFile:"%~dp0Updated2012winre\winre.wim" /Index:1 /MountDir:"c:\temp\mount"

pause
rem Copy iscsi files including iscsi GUI. There may be several redundant files. When running the script seeing Access Denied is normal as files that are already there are not copied.
xcopy "%~dp02012IscsiFiles\*" c:\temp\mount\windows\System32 /E /R /H /Y /C
pause
rem Add Networking batch file to WinRE
copy "%~dp0netcmds\*" c:\temp\mount\Windows\System32 /Y
pause
rem add packages from 2012 ADK, , there may be several redundant packages
Dism /Add-Package /Image:"c:\temp\mount" /PackagePath:"%~dp02012packages\WinPE-WMI.cab"
Dism /Add-Package /Image:"c:\temp\mount" /PackagePath:"%~dp02012packages\en-us\WinPE-WMI_en-us.cab"
Dism /Add-Package /Image:"c:\temp\mount" /PackagePath:"%~dp02012packages\WinPE-NetFx4.cab"
Dism /Add-Package /Image:"c:\temp\mount" /PackagePath:"%~dp02012packages\en-us\WinPE-NetFx4_en-us.cab"
Dism /Add-Package /Image:"c:\temp\mount" /PackagePath:"%~dp02012packages\WinPE-Scripting.cab"
Dism /Add-Package /Image:"c:\temp\mount" /PackagePath:"%~dp02012packages\en-us\WinPE-Scripting_en-us.cab"
Dism /Add-Package /Image:"c:\temp\mount" /PackagePath:"%~dp02012packages\WinPE-PowerShell3.cab"
Dism /Add-Package /Image:"c:\temp\mount" /PackagePath:"%~dp02012packages\en-us\WinPE-PowerShell3_en-us.cab"
Dism /Add-Package /Image:"c:\temp\mount" /PackagePath:"%~dp02012packages\WinPE-StorageWMI.cab"
Dism /Add-Package /Image:"c:\temp\mount" /PackagePath:"%~dp02012packages\en-us\WinPE-StorageWMI_en-us.cab"
pause
Dism /Unmount-Image /MountDir:c:\temp\mount /Commit
pause
2012 R2 Batch File contents:

rem For 2012 R2
rem mount winre from 2012 R2 media (that's inside install.wim)
md c:\temp\mount
Dism /Mount-Image /ImageFile:"%~dp0Updated2012r2winre\winre.wim" /Index:1 /MountDir:"c:\temp\mount"
pause
rem Copy iscsi files including iscsi GUI. There may be several redundant files. When running the script seeing Access Denied is normal as files that are already there are not copied.
xcopy "%~dp02012r2IscsiFiles\*" c:\temp\mount\windows\System32 /E /R /H /Y /C
pause
rem Add Networking batch file to WinRE
copy "%~dp0netcmds\*" c:\temp\mount\Windows\System32 /Y
pause
rem add packages from 2012 R2 ADK, there may be several redundant packages
Dism /Add-Package /Image:"c:\temp\mount" /PackagePath:"%~dp02012r2packages\WinPE-WMI.cab"
Dism /Add-Package /Image:"c:\temp\mount" /PackagePath:"%~dp02012r2packages\en-us\WinPE-WMI_en-us.cab"
Dism /Add-Package /Image:"c:\temp\mount" /PackagePath:"%~dp02012r2packages\WinPE-NetFx.cab"
Dism /Add-Package /Image:"c:\temp\mount" /PackagePath:"%~dp02012r2packages\en-us\WinPE-NetFx_en-us.cab"
Dism /Add-Package /Image:"c:\temp\mount" /PackagePath:"%~dp02012r2packages\WinPE-Scripting.cab"
Dism /Add-Package /Image:"c:\temp\mount" /PackagePath:"%~dp02012r2packages\en-us\WinPE-Scripting_en-us.cab"
Dism /Add-Package /Image:"c:\temp\mount" /PackagePath:"%~dp02012r2packages\WinPE-PowerShell.cab"
Dism /Add-Package /Image:"c:\temp\mount" /PackagePath:"%~dp02012r2packages\en-us\WinPE-PowerShell_en-us.cab"
Dism /Add-Package /Image:"c:\temp\mount" /PackagePath:"%~dp02012r2packages\WinPE-StorageWMI.cab"
Dism /Add-Package /Image:"c:\temp\mount" /PackagePath:"%~dp02012r2packages\en-us\WinPE-StorageWMI_en-us.cab"
pause
Dism /Unmount-Image /MountDir:c:\temp\mount /Commit
pause

Following the successful completion of the appropriate batch file, you should have an updated winre.wim file in the predefined Updated<OS>winre directory. 

To create a bootable .iso from the updated winre.wim:
- I chose to open up a MDT .iso I had on file with my favorite .iso utility
- rename the updated winre.wim to boot.wim
- use my favorite .iso utility to overwrite the boot.wim within the .iso and re-save the .iso

There are many ways to create a bootable .iso so you may want to use another method if you're more comfortable with it.

If you are restoring a physical server you may need to burn a CD/DVD from the .iso. At this point you're ready to restore your target server by:
- boot up the server with the .iso or CD/DVD
- choose 'Command Line'
- execute the netdhcp.cmd file to start networking. Note if you need a static IP, you can use the syntax that's remarked out in the batch file.
- setup the iSCSI connection with the iSCSI GUI
- go back into 'System Image Recovery' and perform the bare metal restore! This process works great to restore virtual servers as well.

Hopefully this was helpful and you are working successfully with 2012/2012R2 and iSCSI bare metal restore!

On a final note, while I was researching this solution I found the 2008 R2 TechNet article that included iSCSI WinPE integration https://technet.microsoft.com/en-us/library/ee619769(v=ws.10).aspx. I found that when using the registry additions referenced in the 2008 R2 article in 2012/2012 R2 WinXE environments, iSCSI would work correctly, but bare metal restoration would fail with an error.