Copy to mounted VHD with PowerShell for automated Hyper-V Provisioning

I have been working on an automated provisioning script for Hyper-V and have been annoyed that there is no way to get the associated drive letter for a mounted VHD based on the VHD path. I have read a few posts from around the net for older versions of PowerShell but on Server 2012 they do not seem to work.

At this point, I have given up trying to find a way to get a unique drive letter, and instead just get the drive letters for any attached VHD. The script I am working on will only continue if there is only one VHD attached. This is a requirement as if there were multiple VHDs attached, I do not know how I can identify which drive letter is assigned to which VHD.

The goal is to have the VHD mounted, and then have a BAT script generated containing IP address configuration information, which, once unmounted and used in my main provisioning script will allow the user to ‘automatically’ set the VM IP address. The BAT (Or PowerShell – haven’t worked out if the security settings will allow for a PowerShell script to run on start-up) script will then run on the first boot of the VM and set the correct IP address, gateway and so on.

 

http://www.nexco.com.my/images/wh2012.png

 

In the example below (which I will be updating from time to time) I have created a script that will first ensure that there are no more than one VHDs mounted. It will then create/check a lockfile before continuing. If the lockfile is unlocked, it will echo “Copying to VHD”. At this point it is just an echo of text and continue. I have yet to work out the BAT format for setting the IP correctly.

I will be aiming to have the BAT file configured before it hits the copy stage. This will probably just be another function that creates the file. Then in the copyToVhd function, it will mount the associated VHD, copy the BAT file to a start-up folder (or something of the sort) and then unmount the VHD. All with a fairly decent level of protection against having the copyToVhd function being run by different people at the exact same time (which would make it impossible to copy the BAT file to the correct VHD)

When I have some more time to work on it, Ill probably be more flexible with the first check and have it as a while loop to allow for cases where the VHD may not have been detached from another script in progress.

Anyway, this is what I have so far: *Forgive my total and utter messiness – I have no formal PowerShell training, and have only been playing with it for a week!

 

If anyone has any idea on how to get a drive letter based on the mounted VHD I would be super happy! This would mean the lock file wouldn’t be necessary, as I could identify which drive letter to copy to, and unmount based on that also.

Tags: , , , , , , , , ,

2 Responses to “Copy to mounted VHD with PowerShell for automated Hyper-V Provisioning”

  1. Adam Says:

    It seemed like there had to be an easier way to get the drive letter and I finally found it. The last example in get-help mount-vhd -examples says you can do the following: PS C:\>Mount-VHD –Path c:\test\testvhdx –PassThru | Get-Disk | Get-Partition | Get-Volume

    I didn’t know what -Passthru did, but apparently it is very useful. To just get the drive letter you can simplify the command to (Mount-VHD –Path c:\test\testvhdx –PassThru | Get-Disk | Get-Partition).DriveLetter

  2. Chris Says:

    Hi Adam,

    I eventually worked something out and ended up using this:

    Mount-VHD -Path PATH -PassThru | Get-Disk | Get-Partition | Get-Volume | Where-Object {$_.FileSystemLabel -ne ‘System Reserved’} | Select DriveLetter

    Overly complex compared to your example, but it worked :)

Leave a Reply