The Deployment Bunny

OS Deployment, Virtualization, Microsoft based Infrastructure…

  • Archives

  • Meta

PowerShell is King – Convert WIM to VHD/VHDX with support for Gen 1 and Gen 2 (BIOS/UEFI) and then some…

Posted by Mikael Nystrom on December 19, 2013

In my line of work, automation is important, since time is the only thing I don’t have. Many years ago I created the WIM2VHD script and that was ok, so when R2 came out I needed to update it to support UEFI. That was easier said then done. The problem is that the UEFI boot process requires fat32, GPT and a bunch of partitions and when using PowerShell to create them the OS will immediately start protecting them after creation which makes it impossible to copy files to the boot partition since it is “protected”, but since I’m a very creative person I found the solution in diskpart. (read about it further down)

Mounting the ISO

I actually have to scripts in this case, one is to mount the ISO that will return the drive letter, kind of handy to have since I need to know the path to the wim file

image

The script to mount the ISO

image

The script to mount the ISO (Text)

————————————————-

# Mount ISO and get driveletter
Param(
[parameter(mandatory=$true,HelpMessage="Please, provide a path to ISO.")]
[ValidateNotNullOrEmpty()]
$ISO
)
Mount-DiskImage -ImagePath $ISO
Write-host (get-date -Format u)" – Mounting =" $ISO
$ISOImage = Get-DiskImage -ImagePath $ISO | Get-Volume
$ISODrive = [string]$ISOImage.DriveLetter+":"
Write-host (get-date -Format u)" – Driveletter is now =" $ISODrive

———————————————–

Getting the correct Index number in the WIM

After the ISO is mounted you can use the Get-WindowsImage commandlet to see what Indexes you have in the WIM file.
The Command Get-WindowsImage – Path d:\sources\install.wim will give me this list.

image

 

Converting the WIM file to the VHDX file

Besides converting the WIM to a VHDX file it also creates the UEFI style disk layout or the BIOS style disk layout. It also give you the opportunity to add the SXS folder to the drive (makes it a bit easier to add .Net framework without internet access or media access) and it will also ask you to add a Tools folder, if you do it will end up in c:\Tools, extremely handy, I usually put things in there that I need, NP++, Sysinternals, 7-Zip, and such.

If you open the script you will one thing that is a bit strange (actually two), when creating the UEFI style disk partition we create the first partition with the WRONG GUID number and that is the way we need to do it, otherwise windows will start protecting the drive, so we create the partition with the GUID for data drive, later on we create a text file and we let diskpart run against that text file that will then change the GUID number to the correct one. scary…

Sample command to create a BIOS based VHDX

.\Create-VHDX.ps1 -SourceFile d:\sources\install.wim -DestinationFile C:\Test\RWS2012R2BIOS.vhdx -Disklayout BIOS -Index 2 -PathtoSXSFolder d:\sources\sxs -PathtoExtraFolder C:\HYDV4\Tools –Verbose

Sample command to create a UEFI based VHDX

.\Create-VHDX.ps1 -SourceFile d:\sources\install.wim -DestinationFile C:\Test\RWS2012R2BIOS.vhdx -Disklayout UEFI -Index 2 -PathtoSXSFolder d:\sources\sxs -PathtoExtraFolder C:\HYDV4\Tools –Verbose

When running it looks like this

I have tested the script on WS2012R2/W8.1 with Hyper-V but it should work on WS2012/W8 as well, just that I don’t have any of those old systems running at home anymore…

image
Since the script is 255 lines of POSH, you better download it from here http://sdrv.ms/1foJ3Wg
/mike

10 Responses to “PowerShell is King – Convert WIM to VHD/VHDX with support for Gen 1 and Gen 2 (BIOS/UEFI) and then some…”

  1. thewozmobile said

    I get this error running the CREATE-VHDX Script:

    [2013-12-29T16:26:48.4268320-05:00] [CheckDestinationFile - Apply boot files][]
    cmd : BFSVC Warning: Resource files missing from H:\Windows\boot\Resources. These files are required for some editions of Windows. If you
    are servicing older versions of Windows, you can ignore this message.
    At C:\users\dwoznicki\Downloads\Create-VHDX\Create-VHDX.ps1:225 char:1
    + cmd /c “$VHDXVolume3\Windows\system32\bcdboot $VHDXVolume3\Windows /s $VHDXVolum …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (BFSVC Warning: …e this message.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

    Another Question

    What belongs in c:\HYDV4\Tools?

    • Verify that the WIM file you have is 100% correct, it is VERY common to have busted downloads, even IF they seems to be working, happens at least once a month. Also, check the screen for drive letters on all drives, there should be logic in there that protects this kind of behavior. On what OS are you running btw?

  2. thewozmobile said

    BTW. I was trying to create a UEFO VDHX of Windows Server 20012 R2 Datacenter Standard

  3. thewozmobile said

    Thanks Mikael. I downloaded the image again and ran it fine.

  4. thewozmobile said

    Hey Mikael, A couple suggested changes the Get-WindowsImage – Path d:\sources\install.wim Should be: Get-WindowsImage -ImagePath d:\sources\install.wim at least on Windows 8.1 with Powershell 4. Also in the sample command line changing the file name to C:\Test\RWS2012R2UEFI.vhdx would allow you to create both files without the risk of deleting it. Just a thought.

  5. […] PowerShell is King – Convert WIM to VHD/VHDX with support for Gen 1 and Gen 2 (BIOS/UEFI) and … […]

  6. […] Convert WIM to VHD/VHDX with support for Gen 1 and Gen 2 (BIOS/UEFI) http://deploymentbunny.com/2013/12/19/powershell-is-king-convert-wim-to-vhdvhdx-with-support-for-gen…/ […]

  7. Milan Banika said

    Thank you Mikael Nystrom. Works like a charm.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 3,866 other followers

%d bloggers like this: