Howdy folks,
We’ll have to make this a quick post, since I am only on for a 15 mins break at the SPOC 2010 conference
. I prepared a few things, and now I am putting them together.
First of all, I hope you enjoy as much as I do the concept of having VHDs under Windows 7. For those who don’t know where to get started, go to Computer –> Manage –> Disk Management (shown in Figure 1)
Figure 1: Disk management. Right-click in order to create or attach VHDs.
And this is where the problem resides. Everytime you want to mount your disks, you have to enter here and execute “Attach Vhd” manually. Not cool enough for us.
The solution: A script, executed as scheduled task (run as Administrator)
These are the steps:
- Create a script that calls the diskpart commands
- Call the script from a batch file
- Create a scheduled task for it.
Sounds simple? It is!
1. Create a script that calls diskpart commands.
Figure 2: The attachScript.txt script file.
2. Call the script from a batch file
Figure 3: The corresponding batch file: Just calling diskpart with the /s switch and the script file from Figure 2
3. Create a scheduled task for it
Step 1: Start –> Accessories –> System Tools –> Task Scheduler
Step 2: Set the following fields (Tab: General):
- Name: Choose what you like. Here: “Mount Vhds”
- Security options (1): Change User or Group –> Choose an Administrator!
- Security options (2): Run whether user is logged on or not – makes sense. We don’t need the admin to be logged on for us to access our Vhd, right?
- Security options (3): Do not store password. You can optionally set this.
Step 3: Set “Begin the task” to At startup. Now our task will run at startup.
Step 4: Specify which file to run (our created batch/AttachVhds.bat). Confirm by clicking on OK.
That’s it! We’re done! From now on, each time we start the computer, our created task will run!
Important: This is not the most elegant way of automating such a thing, but at the moment there is not a lof of choice under Windows 7. For Windows Server 2008 R2, you can also perform this functionality using PowerShell (and not diskpart/batch files etc.).
I hope you enjoyed this quick tutorial!
Cheers & best greetings from Milan,
Martin


Hi Martin,
this is a great post. I already tryied it successfully. I created a new post with a little improvement of your technique. The post explains how to automate the process of autoaddition with PowerShell. You can find the entry here: http://patricklamber.blogspot.com/2010/03/windows-7-auto-mount-your-vhd-disks-at.html
Best regards,
Patrick Lamber
Nevermind, got it!
Hi,
nice post! and works well!
I would like to assign a specific letter during attaching and using this modification does not work:
select vdisk file=”E:\VHD\install.vhd”
attach vdisk
assign letter=i
the first available letter ist selected automatically instead – therefor diskpart ignores the assign command?
Could you give me a hint?
Cheers,
Markus
One slight flaw and one comment,
The post says that you should choose an Adminstrator to run the scheduled task. The only account I’ve found that is enabled by default and can bypass UAC, and thus work, is SYSTEM.
The .BAT file is actually unneccesary, though comforting. You can run diskpart as the direct action of the Scheduled Task. The path to diskpart goes in Program and the “-s “” goes in Add Arguments
Now if I can figure out how to disable AutoRun for just the one drive…
Thanx, it helped a lot!
Frank,
I too struggled with disabling auto-run till I found the following article:
http://www.wikipedia.org/wiki/autorun
Where you can specify the correct Hex value for “NoDriveAutoRun” according to the letter drive you want to squash prompting for. Example if my auto-mount is drive “G” then the binary value for G (being the 7th letter of the alph) is “1000000″, and HEX is F4240. You can use the Calculator in Program to convert Binary to HEX.
btw I used the auto-mount of VHD to overcome Win7 Home Premiums limitation of backing up to a network drive, however mine now MOUNTS a network drive and uses the local drive letter for backup
woot woot!
Hope that helps!
Michael
I created my bat file like your but was getting a 0×1 on my task and the vhd’s where not attached but the bat would run find be its self. It seems I was hitting a bug because if the “” on the path in the bat, once removed the task runs now with out problem.
[...] we need to create the automation to attach the VHD at startup. You can get a howto here http://angler.wordpress.com/2010/03/11/windows-7-auto-mount-vhds-at-startup/ the comments also have away to do it with [...]