none
Automate VHD attachment at system start-up? RRS feed

  • Question

  • I use a couple of VHDs on my Windows 7 PC. But the issue that I'm facing is:

    • Every time I shutdown my PC, the VHDs detach and do not re-attach on next start-up. I have to do it manually. How do I automate this task?
    • Are there scripts that I can double-click which automatically attach and detach the VHD?

    I usually have to go to Disk Management or use 'diskpart' to do it every single time. Thank you for your time!


    Kunal D Mehta - a Windows Server Enthusiast.


    Saturday, August 13, 2011 3:14 PM

Answers

  • Well I'm a bit at a loss here I have a feeling we may not be able to do it the way we have been, but this link to all the diskpart commands might help you create a better setup.

    http://technet.microsoft.com/en-us/library/bb490893.aspx

     

    Didn't even think of Powershell but there is a possibility that may work.

    Try this for your powershell script:

    $objVHDService = get-wmiobject -class "Msvm_ImageManagementService" -namespace "root\virtualization" -computername "."

     

    $objVHDService.Mount("e:\virtual\test\test.vhd")

     

    FORGOT TO MENTION! the above are 2 separate scripts so you may still need a small bat file to run the first one then the second one a few secs later.

     


    Please remember to flag a post that helps you as "answer" so that it may help others with same problems.

    • Edited by DirtyOleBeggar Sunday, August 14, 2011 7:43 PM added powershell script lines
    • Marked as answer by Juke ChouModerator Sunday, September 4, 2011 1:23 PM
    Sunday, August 14, 2011 7:30 PM
  • Well I'm a bit at a loss here I have a feeling we may not be able to do it the way we have been, but this link to all the diskpart commands might help you create a better setup.

    http://technet.microsoft.com/en-us/library/bb490893.aspx

    If you take a look at the bottom of that page, you will see that diskpart supports scripting, which should allow you to automate the VHD attachment. Otherwise said: you need a diskpart script, not a batch file.

    Tim De Baets
    http://www.bm-productions.tk
    Tuesday, August 16, 2011 12:50 PM
  • Hi,

    Also, if you need to compose a script. You may post the thread at http://social.technet.microsoft.com/Forums/en/ITCG/threads/ for help.

    Thanks for your understanding.


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
    Wednesday, August 17, 2011 9:36 AM
    Moderator

All replies

  • I use a couple of VHDs on my Windows 7 PC. But the issue that I'm facing is:

    • Every time I shutdown my PC, the VHDs detach and do not re-attach on next start-up. I have to do it manually. How do I automate this task?
    • Are there scripts that I can double-click which automatically attach and detach the VHD?

    I usually have to go to Disk Management or use 'diskpart' to do it every single time. Thank you for your time!


    Kunal D Mehta - a Windows Server Enthusiast.


    Adding a Boot Entry for a VHD File

    When you have created a VHD and installed a system image on it, you can use the BCDEdit
    tool Bcdedit.exe to add a boot entry for the VHD file in your computer running Windows 7.
    This high-level procedure is as follows:

    Open the elevated command prompt with Administrator privileges and enter a command
    similar to the following: bcdedit /copy {current} /d "Your New VHD Description"
    This returns the GUID of the loader object. You use this value to replace the variable
    <guid> in the following commands:
    bcdedit /set <guid> device vhd=[driveletter:]\<directory>\<vhd filename>
    bcdedit /set <guid> osdevice vhd=[driveletter:]\<directory>\<vhd filename>

    BCDEdit locates the VHD file and Bootmgr locates the partition containing the VHD File to
    boot from. Finally, you enter the command: bcdedit /set <guid> detecthal on
    Detecthal is used to force Windows 7 to automatically detect the Hardware Abstraction
    Layer (HAL).

    The following command tests if your boot entry is successfully created: bcdedit /v

    If you want to delete an existing VHD entry from the Boot menu, you use the following
    command: bcdedit /delete <guid> /cleanup
    This deletes the specified operating system entry from the store and removes the entry
    from the display order.

    When you restart your computer after successfully completing this procedure, you should
    see an additional entry in the Boot menu along with the default Windows 7 operating system.

    Best of luck to you!


    Please remember to flag a post that helps you as "answer" so that it may help others with same problems.
    Saturday, August 13, 2011 3:51 PM
  • Thank you for your response. But this VHD is not a system volume. It's just a simple volume that I use to store important stuff and multimedia files.

    Do we have to use the bcdedit command even when there is no OS on the volume? I've done the above previously, booted from a VHD. But the above scenario (my first post) is not about a VHD boot. So, how do I go about it now?


    Kunal D Mehta - a Windows Server Enthusiast.
    • Edited by Server Baba Saturday, August 13, 2011 4:50 PM eliminated ambiguity
    Saturday, August 13, 2011 4:44 PM
  • I pulled this quote below off of one of the MS Technet pages

    When an operating system restarts, VHDs that were attached before reboot are not automatically attached. If you native boot to a VHD, only the file system partitions contained in the VHD are automatically attached. If there are other VHDs on the physical volume that were attached during a previous boot, they are not automatically attached.

    Original webpage for the above info:

    http://technet.microsoft.com/en-us/library/dd440865%28WS.10%29.aspx#two

    So it would appear that while the VHD can not stay attached you may attach the volumes within it although I can't see how they stay attached myself.


    Please remember to flag a post that helps you as "answer" so that it may help others with same problems.
    Saturday, August 13, 2011 5:11 PM
  • Ok, so this answers my first question.

    Can we write a script and save it in a file so that when I double-click it, the script runs and the VHD gets attached?

    The usual command to attach VHDs is as follows:

    C:\> diskpart

    diskpart> sel vdisk file = C:\folder1\name.vhd
    diskpart> attach vdisk

    I tried creating a batch file with all the 3 commands, but only the 1st (diskpart) command gets executed. The other two don't. Any idea?


    Kunal D Mehta - a Windows Server Enthusiast.
    • Proposed as answer by HundotBoy Thursday, October 24, 2013 9:07 AM
    Saturday, August 13, 2011 5:45 PM
  • When you do a "list disk" in diskpart does is show the VHD or is it listed as a volume?

    IE when you do list disk is shows actual hard drives

    so I think you would sel disk X

    then sel volume


    Please remember to flag a post that helps you as "answer" so that it may help others with same problems.
    Saturday, August 13, 2011 6:42 PM

  • Kunal D Mehta - a Windows Server Enthusiast.
    Saturday, August 13, 2011 6:53 PM
  • I believe you need to add a wait state of say roughly 5 secs in your batch file before allowing it to continue to the next command where you select the VHD. That way Diskpart will be able to load completely before the next command is issued.

    I've noticed it takes diskpart a couple seconds to load this is why I mentioned it.


    Please remember to flag a post that helps you as "answer" so that it may help others with same problems.
    Saturday, August 13, 2011 8:27 PM
  • Yes, that does take a couple of seconds. I'm not sure how do we add a wait state. Can you help me with the exact code that I'll have to punch in to the batch file? I tried looking a couple of articles about 'wait states' but found nothing useful. Thank you!
    Kunal D Mehta - a Windows Server Enthusiast.
    Saturday, August 13, 2011 8:37 PM
  • *dusts cobwebs of that storage section of his brain*

     

    I believe the Syntax for a delay is something like:

    Timeout (delay in seconds "I think might be milli secs" 1-100000)

    so in your case it may go something like:

    C:\diskpart

    timeout 5

    diskpart sel vdisk file = c:\folder1\name.vhd

    timeout 2

    diskpart attach vdisk name.vhd

    I haven't wrote a batch file in soooo long hope this works for you :)

    (not responsible for hardware blowing up)

    HEY! if you want to be lazy about it use this utility

    http://www.ghacks.net/2009/04/26/windows-batch-file-creation/

     

    Just reread this last edit, It sounds like I'm insulting you but trust me it was not ment that way. I just figured a nice little program that coughs out preformed bat files was a nice lazy way to make them. anyhow hope it all worked out for you.


    Please remember to flag a post that helps you as "answer" so that it may help others with same problems.

    • Edited by DirtyOleBeggar Sunday, August 14, 2011 12:52 AM added last paragraph.
    Saturday, August 13, 2011 9:43 PM
  • I tried the first solution that you gave, adding the 'timeout 5' values into the batch script, but unfortunately it didn't work.. :(

    As for the second solution, the description on the target page of the URL says:

    "The batch files can be saved directly to the computer system and even the Windows startup process. They are however not saved as .bat files but as .brs files which can only be executed by the Batchrun software."

    And I'm very paranoid about running 3rd party software. I'm sure there has to be a way to do it without them. But don't know how!

    PS - I found nothing that you wrote as insulting. In fact, I appreciate your patience in taking your precious time to reply to my queries! :)


    Kunal D Mehta - a Windows Server Enthusiast.
    Sunday, August 14, 2011 8:19 AM
  • Instead of using timeout try using either WAIT or PAUSE. I'm not sure these commands work in the current form of the command prompt but worth a try (they used to be valid commands in DOS).

    Also you might want to add @ECHO at the beginning (till you get it working right) so it will display what its trying to do as it does it.

    The @ sign will stop the ECHO command from showing but the rest of the commands will be displayed, once everything is working in your batch file the way you want you can just add the word off after echo (@echo off) and nothing will be displayed from that point on.

    PAUSE will stop the batch file until you hit a key to make it continue.

    ALSO alternatively that program that gave you a batch file with the .brs extension, try opening it in notepad to edit it, if it looks normal and there isn't anything you need to remove then just change the extension to .bat.

     

    NOTE: you can also use the file extension .cmd instead of .bat


    Please remember to flag a post that helps you as "answer" so that it may help others with same problems.
    • Edited by DirtyOleBeggar Sunday, August 14, 2011 1:36 PM added NOTE:
    Sunday, August 14, 2011 1:29 PM
  • Tried WAIT, PAUSE and @ECHO. Didn't help. They do work with other commands, but not with the 'diskpart', 'sel vdisk file' and 'attach vdisk' combination. Any other ways to go about this, like a Powershell script or something?
    Kunal D Mehta - a Windows Server Enthusiast.
    Sunday, August 14, 2011 6:47 PM
  • Well I'm a bit at a loss here I have a feeling we may not be able to do it the way we have been, but this link to all the diskpart commands might help you create a better setup.

    http://technet.microsoft.com/en-us/library/bb490893.aspx

     

    Didn't even think of Powershell but there is a possibility that may work.

    Try this for your powershell script:

    $objVHDService = get-wmiobject -class "Msvm_ImageManagementService" -namespace "root\virtualization" -computername "."

     

    $objVHDService.Mount("e:\virtual\test\test.vhd")

     

    FORGOT TO MENTION! the above are 2 separate scripts so you may still need a small bat file to run the first one then the second one a few secs later.

     


    Please remember to flag a post that helps you as "answer" so that it may help others with same problems.

    • Edited by DirtyOleBeggar Sunday, August 14, 2011 7:43 PM added powershell script lines
    • Marked as answer by Juke ChouModerator Sunday, September 4, 2011 1:23 PM
    Sunday, August 14, 2011 7:30 PM
  • Well I'm a bit at a loss here I have a feeling we may not be able to do it the way we have been, but this link to all the diskpart commands might help you create a better setup.

    http://technet.microsoft.com/en-us/library/bb490893.aspx

    If you take a look at the bottom of that page, you will see that diskpart supports scripting, which should allow you to automate the VHD attachment. Otherwise said: you need a diskpart script, not a batch file.

    Tim De Baets
    http://www.bm-productions.tk
    Tuesday, August 16, 2011 12:50 PM
  • Hi,

    Also, if you need to compose a script. You may post the thread at http://social.technet.microsoft.com/Forums/en/ITCG/threads/ for help.

    Thanks for your understanding.


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
    Wednesday, August 17, 2011 9:36 AM
    Moderator
  • Ok, let me just try the solutions that DirtyOleBeggar and Tim De Baets proposed. If they don't work I'll request you to move the thread to an appropriate location. I've been very busy lately, so just need another day or two, thanks!


    Kunal D Mehta - a Windows Server Enthusiast.
    Thursday, August 18, 2011 5:32 PM
  • A better way (I think) to manage your command-timing using Powershell would be to try launching the DISKPART command (w/script) as a JOB, and then check for the job to finish before continuing with your next operation.  Sorry, don't have the time to detail the process, but there are many examples out there of using jobs with PS if you Google it (that's how I learned it).

     

    -wwar1ace

     

    Instead of using timeout try using either WAIT or PAUSE. I'm not sure these commands work in the current form of the command prompt but worth a try (they used to be valid commands in DOS).

    Also you might want to add @ECHO at the beginning (till you get it working right) so it will display what its trying to do as it does it.

    The @ sign will stop the ECHO command from showing but the rest of the commands will be displayed, once everything is working in your batch file the way you want you can just add the word off after echo (@echo off) and nothing will be displayed from that point on.

    PAUSE will stop the batch file until you hit a key to make it continue.

    ALSO alternatively that program that gave you a batch file with the .brs extension, try opening it in notepad to edit it, if it looks normal and there isn't anything you need to remove then just change the extension to .bat.

     

    NOTE: you can also use the file extension .cmd instead of .bat


    Please remember to flag a post that helps you as "answer" so that it may help others with same problems.

    Wednesday, January 4, 2012 10:38 PM
  • I am new to this forum and not sure if the problem was solved yet but I have been working on a solution all afternoon and wanted to share with everyone.

    To ensure that my VHD re-attached itself at startup I used a combination of the diskpart command and the Task Scheduler. I also followed part of the instructions per the link description below, although I wrote a batch file and it still worked even though it was not a .exe file

    Google: How to Geek - create administrator mode shortcuts without uac prompt (This should point you in the right direction for the link)

    Here are the steps I took to ensure that I was able to have access to my VHD upon startup each time.

    • I created a simple folder on the c:\ drive named "Windows Scripts" (you can name it anything you like)
    • Next I created (2) files, the first being a batch file and the next being a .txt file that the diskpart script use to help execute the command.

    The batch file contained the following line which linked itself to a text file:

    diskpart /s "c:\windows logon scripts\practicevhd.txt"

    The text file that I created next that is linked to the batch file has the following two lines associated with it:

    select  vdisk file "c:\windows vhd\practicevhd.vhd (this is what I called my practice VHD file -  you can name it anything)

    attach vdisk

    Then I used the task scheduler as per the link above to schedule the task. I had to play with some of the settings and did not bother creating the shortcut. For each tab, these are the settings I used to successfully mount the VHD every time.

    GENERAL: I chose to "run whether the use was logged on or not" - I also chose to "run with the highest privaledges" (I found out that when I chose to run the program when I was logged in the VHD did not mount)

    TRIGGERS: I chose to run the program at system startup

    ACTIONS: You have to naviate and find your program, then make sure the Action is "Start a program"

    This  should do it. If you have any questions please feel free to post them and I will help if I can.

    One last detail, I had to go into the Autoplay settings and under "Mixed Content", I had to choose "Take no Action" so that each time I started my PC up the "What do you want to do" dialog box did not show up along with the VHD applet.

    Cheers!

    -Paul

    Sunday, February 24, 2013 11:03 PM
  • Hi PaultheStudent

    Thanks for your solution above.  I had exactly the same problem and it works perfectly

    Saturday, May 25, 2013 6:23 PM
  • Took me forever to find the command, but if you have PowerShell v3 installed you can use Mount-DiskImage.

    http://technet.microsoft.com/en-us/library/hh848706(v=wps.620).aspx

    Simple as:

    Mount-DiskImage -ImagePath 'E:\virtualdisks\mydatadisk.vhdx'

    I haven't tried it in a startup script, but the command itself works fine.

    Monday, June 24, 2013 5:43 PM
  • F... all the Microsoft Engineers  that they have written a S/W that cannot keep a simple drive attached after after reboot.

    F..  all of the hundred more time

    STLAZAR23

    Monday, February 18, 2019 4:44 AM