Can't Start Virtual Machine After Overwriting Virtual Hard Disk

I have been working on a personal project recently.  My goal is to completely automate the process of building a virtual machine - without using templates.

As I have been working on this process, I have been making and deleting the same virtual hard disk over and over. 

This causes an interesting problem.  If you have a virtual machine all configured and ready to go - but you then replace the virtual hard disk file (i.e. delete the current .VHD file and create a new one in the same place with the same name) - when you try to start the virtual machine it will fail.

Instead of starting you will get an error message like this:

Capture

The problem here is the permissions on the new virtual hard disk.  When you attach a virtual hard disk to a virtual machine Hyper-V will change the permissions (access rights) on the virtual hard disk.  This needs to be done because Hyper-V virtual machines run under their own account, and this account needs to have permission to access the virtual hard disk.  When you switch the virtual hard disk file of a virtual machine you lose these permissions.

Fixing this problem is simple - but not obvious.  What you need to do is:

  • Open the virtual machine settings (through the Hyper-V manager)
  • Find the virtual hard disk in question
  • Remove the virtual hard disk (use the remove button)
  • Apply the changes
  • Add a new virtual hard disk and configure it to point at the same virtual hard disk as before
  • Apply the changes

This should only take a couple of seconds to do, and it fixes the virtual hard disk permissions so the virtual machine will start again.

Cheers,
Ben