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).
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).
That’s my default presentation, but let’s show a working example:
(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.
- 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.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy