Copy to mounted VHD with PowerShell for automated Hyper-V Provisioning – Part II
While I initially tried to get the Powershell Mount-VHD cmdlet working – I gave up as it didn’t seem to exist (despite obvious documentation: https://technet.microsoft.com/en-us/library/hh848551.aspx)
I was using a Server 2012 DS for testing purposes, but as I was just working on the drive letter side of things – I had yet to install the Hyper-V role. Turns out that Mount-VHD is exactly what I need to get the driveletter of a VHD and requires Hyper-V to be installed. The last example on the Microsoft Site gives you all you need to know:
Mount-VHD –Path c:\test\testvhdx –PassThru | Get-Disk | Get-Partition | Get-Volume
With a little modification we can ensure that the System Reserved partition does not get in the way:
Mount-VHD -Path PATH -PassThru | Get-Disk | Get-Partition | Get-Volume | Where-Object {$_.FileSystemLabel -ne 'System Reserved'}
This is what you will get after mounting using the above command:
You can then select the driveletter column to get what you need:
Mount-VHD -Path PATH -PassThru | Get-Disk | Get-Partition | Get-Volume | Where-Object {$_.FileSystemLabel -ne 'System Reserved'} | Select DriveLetter
Much more simple than my previous attempt!
Enjoy.
I use this pipeline for mount and determinate drive letter. But sometimes this doesn’t work. Looks like system not always can mount drive or mounting is very long process and get-partition is very quick process, so that pipeline order is violated. I don’t know why. But if I try run this pipeline again, then it is work well.
Does pipeline work fine for you?