Hi all,
Windows 7 provides some nice features including the possibility to create and attach VHD disks natively. This is a great way to organize differently your data and move it back and forth from one system to the other by solely copying your VHD file.
Unfortunately, every time you restart your machine, you loose all VHD attachments that you created before and you have to mount them manually. Martin, created a great post and explains there how to create a script that does the job for you. In this post I’m going to explain how we can automate this task with PowerShell.
In my case, I want that all VHD disks that I have in one of my directories are automatically attached on every startup. Therefore, I followed Martin’s instructions and got everything working nicely. The only drawback of this solution is the manual adding of each single VHD entry I created. That means that every time I delete or add a new disk, I have to modify the script that was created. In most cases this is not an issue, however, I wanted a more immediate and dynamic solution.
Because of that, I created a PowerShell script that takes all .vhd files in the directory it resides and attaches them by using diskpart. At the end, I only automated the step of the manual script definition in Martin’s post. The PowerShell code snipped that does the trick is described below:
foreach ($file in dir *.vhd) {
$command= @"
select vdisk file=$file
attach vdisk
"@
$command | diskpart
}
The logic is simple: loop over all files with *.vhd extension and execute the command in diskpart. Last but not least, you have to save this function in your .vhd folder and call it in your startup folder. I solved it by creating a .bat file that calls the PowerShell function like that:
powershell.exe ./AttachVHD.ps1
Every time you add a VHD disk in the directory the script resides, the script attaches automatically this disk to your system.
Martin’s blog for creating a scheduled task.
Hope this helps,
Patrick


Hi Shane,<br />which version of Windows are you using? Do you have Powershell enabled?<br /><br />br,<br />Patrick
This is what I get.<br /><br />The string starting:<br />At D:vhdsattachvhd.ps1:2 char:14<br />+ $command= &lt;&lt;&lt;&lt; @&quot;<br />must have the terminator: &quot;@ at the beginning of the line.<br />At D:vhdsattachvhd.ps1:5 char:5<br />+ &lt;&lt;&lt;&lt; &quot;@<br /> + CategoryInfo : ParserError: ( select … diskpart<br />}<br />:String) [], ParseException<br /> + FullyQualifiedErrorId : WhitespaceBeforeHereStringFooter
Windows 7 Home Edition
Hi, <br />I was able to reproduce the problem. If you copy 1to1 the above script into notepad, then some whitespaces are saved into your file. This causes the exception that you specified before.<br /><br />To circumvent this problem just remove all spaces in the command before &quot;@. Also try to avoid spaces or special characters in the names of your VHD files.<br /><br />br,<br />patrick
Call CMD from powershell then use Diskpart. You should not get errors, Powershell makes the Command console very easy to control with normal functions.
Again, great post. You know, I am super delighted I found this. I'll pin your blog so my friends can read your writing too!