Use PowerShell to Create VHDs for Hyper-V

Use PowerShell to Create VHDs for Hyper-V

  • Comments 3
  • Likes

Summary: Microsoft Windows PowerShell MVP, Marco Shaw, illustrates how to use a graphical Windows PowerShell script to create VHDs for Hyper-V.

Microsoft Scripting Guy, Ed Wilson, is here. When I was talking to Marco about his way cool script to download TechEd sessions, he mentioned that he had been playing around with Hyper-V, and he asked if I was interested in another article. But of course, I replied. Marco is one of the few people that has a standing offer from the Scripting Guys to be a guest blogger. Interestingly enough, Marco anticipates two series of blog posts that I am currently working on.

The first is my Don’t Write Scripts series of blog posts that will begin tomorrow. The other is a Community Resources series of articles that will appear the week of July 11, 2011. (Yes, the Hey Scripting Guy blogs really are planned that far in advance. In fact, they are planned for several months in advance).

Here’s Marco…

Modules are great! Repeat after me…I recently mentioned Doug Finke’s module, OData PowerShell Explorer, and I’m back again with another community module: PowerShell Management Library for Hyper-V. James O’Neill has worked hard on providing this module to the community.

Not only has he provided some excellent code, but he has added Help to it, and an accompanying PDF. James first released this as a snap-in for Windows PowerShell v1. But he kept up with the times, and he has released an updated module for Windows PowerShell v2. He has even updated it for Windows Server 2008 R2 SP1.

So…where was I? Oh yeah! I was asked to create a script that could create VHDs for use with Hyper-V. I think there has been some improvements (new tools) to greatly speed up the creation of VHDs, but a year or so ago, I wasn’t aware of anything. My goal was to write something that could be invoked, then a person could step away (maybe overnight), and my script would chug along, pumping out the requested VHDs.

I decided to wrap up my script with a UI by using Windows Forms (shown in the following image).

Image of Windows Forms

That’s my default presentation, but let’s show a working example:

Image of example form

(Note: The “Not Responding” message showed up when I tried to take a screenshot of the application. Windows Forms can have some threading issues when run from a Windows PowerShell script.)

In the previous working example, you can see:

  • I have my script automatically parse the default Hyper-V location for new VHDs, and that default is also used to create individual VHDs.
  • I can pick a custom size for each VHD.
  • I can enter the information for a VHD, and then simply clear the check box so it isn’t created.
  • I have a progress bar, and also a status box (bottom left corner) that indicates how the VHD creation activity is progressing.

My goal was to ultimately have something very flexible, with good visual feedback, and make it simple enough so that just about anyone could use it. At the time, I chose to use Windows Forms simply because my target operating system was Windows Server 2008. If I rewrote this today, my target would more likely be Windows Server 2008 R2, and I would be looking at using Windows Presentation Framework for the visuals.

Notes:

  • I’ve posted my code online in the Scripting Guys Script Repository. I had some minor issues getting it to run, but I believe the issues are resolved by running it from an elevated shell. 
  • I used Sapien’s PrimalScript 2009 originally.

Thanks, Marco!

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • thanks Marco,

    that is a really nice example of what we could do with Powershell und Windows Forms!

    But it is an example of what I hate most using GUIs:

    You end up with 1452 lines of code and the core functionality is burried in one line

    Invoke-Expression "new-vhd `"$path`" ${size}GB -$type -wait"

    That's a hurdle I won't take ...

    Klaus.

  • This copy of Windows is not genuine??

  • @breakaway

    You got me...  I wipe my computers so many times...  I meant to cut more out of that image, but I guess I sent the wrong image.  Klaus knows I'm not good at last minute checks of my posts...

    Marco