Coding Blog

Jim Scott's coding blog

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
Posted: Nov 26 2009, 10:05 by jscott | Comments (10) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under:

Comments (10) -

Oscar United States said:

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.

# February 10 2010, 19:15

jscott United States said:

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.

# February 10 2010, 22:38

Jim Prusa United States said:

This works only if I set the trigger to at log on rather than at startup.  Windows 7.  Any ideas?

# August 05 2010, 22:06

jscott United States said:

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?

# August 05 2010, 22:19

Jim Prusa United States said:

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!

# August 05 2010, 22:56

K. said:

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!

# June 30 2011, 02:47

Ben Belgium said:

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.

# October 04 2011, 12:19

trevor United States said:

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!

# October 20 2011, 07:07

Howard Sartori United States said:

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

# March 22 2012, 09:50

Kelly R. United States said:

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

# June 28 2012, 14:57

Pingbacks and trackbacks (1)+

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading