Automatically attaching VHD files in Windows 7 and Windows 2008

If you have played with the new VHD feature in windows 7 or windows 2008 then you know just how cool of a feature this is. However the problem is that when you reboot your machine you find that when it comes back up all your VHD files are no longer attached? Here is what I did to get around the issue.

  1. Create a batch file that will hold the following line:

    diskpart /s “c:\path to script\diskpartscript.txt”

    I named my batch file attachvhd.bat and placed it in the same folder as my VHD files


    attachvhd

  2. Create the script file that is being referenced by the attachvhd.bat batch file. Here is what the contents of that script needs to contain:

    select vdisk file="c:\path to vhd files\myvhddrive.vhd"
    attach vdisk

    I named my script file diskpartscript.txt and placed it in the same folder as my VHD files.

    script 

  3. Create a scheduled task that will automatically run when your machine starts up.

    • Go to Start / Administrative Tools / Task Scheduler
    • Click Create Basic Task

      1_addtask

    • Fill in the name of the task and the description and Click Next

      2_createbasictask 

    • Select “Start a program” radio button option and then Click Next

      3_start_program

    • Select “When the computer starts” and then Click Next

      3_when_computer_starts

    • Browse to the folder that you setup your batch file in and select it. Click Next

      5_script

    • Click Finish

      6_finish 


  4. You have now completed all the necessary steps. Restart your computer and you should find that your VHD files are now automatically attached. One caveat is that if you reboot and you log into your machine quick enough it is possible that the task may not have been run yet. Once you are logged in if the task runs you will get an Autoplay dialog as follows. Simply close it. This does not happen if the task runs before you get logged in.

    auto_play

Comments (11) -

  • That's a clever trick. I just wish we didn't have to do these kinds of things and just keep the VHDs mounted at boot, like a network drive. But Thanks for posting this workaround.
  • Agreed, hopefully MS will enhange the VHD support so that one day we can define in disk manager that a VHD should persist after reboots.
  • This works only if I set the trigger to at log on rather than at startup.  Windows 7.  Any ideas?
  • Jim, here is some things to check.

    Edit the scheduled task and go to the General Tab
    Make sure "Run whether user is logged on or not" is selected
    I also have checked "Run with highest privileges"

    Triggers Tab
    Trigger "At Startup"  Status "Enabled"

    See if those settings look the same?
  • Thanks much -- that did the trick.  I had everything correct except I had not checked  "Run with highest privileges."
    Obviously this allows the VHD to install with administrative privileges.
    Thanks much!
  • K.
    Thank you for posting such a useful tutorial! It worked like a breeze after adjusting the admin settings the way Jim and J. suggested for Win 7.

    Thanks everyone for sharing your skills!
  • Ben
    Thank you for this post! However in my case I have to wait about 3 minutes after booting before I can login and have the VHD attached and mounted. When I don't wait long enough it will not have been attached and mounted yet. Is there something I can do to speed up the process of attaching and mounting?

    "Run whether user is logged on or not" and "Run with highest privileges" are both selected.

    On the system run two Windows services (PostgreSQL and MapGuide) which depend on the VHD being attached and mounted because they access their data on it. Because the VHD is not accessible on time these services fail to start and need to be manually started afterwards. The VHD also contains a folder with a web application (IIS). The system is being used for demonstration purposes and the VHD gets copied between a couple of different physical machines. This way updates to the data and web application only need to be applied once on one system and afterwards the VHD gets copied over to the other machines.
  • Ok that works great, but the issue I have is the VHD is a shared drive with permissions, so this does not do much for me because I still have to login and share the drive and add permissions.  Any ideas on how to solve this issue?

    Thanks!
  • Great methods here!  
    But it fails on my PC.  
       Location is not available
       I:\is not accessible.
       The media is write protected.
    What policy do I need to enable, even for Administrators
    Thanks.  /Howard
  • Thanks for this GREAT tip! And to jscott for the additional help! I just created my first VHD as I am learning Wordpress and needed to install Uniserver, so I wanted a VHD. I just restarted today, and I was like O_O. I found a bunch of guides, but they don't explain enough. The language is unnecessarily complicated & redundant on the Windows 7 site.

    Oh and if any1 were to hit my website url, I am in the process of learning Magento and Wordpress to put my store and blog and whatever else there for our new business. <--- not a techie, lol
  • Thanks for the info.  How would I attach multiple VHDs?  

    So far, I'm testing the bat file manually.  My system seems to take a minute or two for the VHD to attach.  Can I just duplicate the select and attach instructions in the same bat file?

    select vdisk file="c:\path to vhd files\myvhddrive1.vhd"
    attach vdisk
    select vdisk file="c:\path to vhd files\myvhddrive2.vhd"
    attach vdisk
    select vdisk file="c:\path to vhd files\myvhddrive3.vhd"
    attach vdisk

Pingbacks and trackbacks (1)+

Add comment

Loading