Hyper-V and Open Source: A Powerful (PowerShell) Combination

CodePlexIn my post last week, I highlighted a number of ways to harness the power of virtualization with little or no costs.  One tool I mentioned was the open source  PowerShell Hyper-V Management library written by James O’Neill available on CodePlex.  This library of PowerShell commandlets can enable you to automate and monitor all sorts of Hyper-V related tasks.  I install and use the library on all of my Hyper-V hosts because it provides me with a simple, consistent command line user interface to access and report on my virtual environment.  There is some documentation available on how to load and use the library online, but I go into more detail in two chapters of our book about Hyper-V.  We’re revising the book right now to cover enhancements in Windows Server 2008 R2 – one of which is the inclusion of PowerShell V2.  The new version of PowerShell includes some cool enhancements you may not know about, like Out-GridView.  I use it with the Hyper-V library to filterable / sortable reports of my virtual environment.  Here are a few quick examples. 

Tough Reporting is Easy!

f1025Data gathering across multiple physical systems can be hard, and sometimes the best way to collect information seems like laborious “cut and paste” torture.   How might you (for example) gather all the MAC addresses from your VM’s virtual network cards for a network audit?  You could access the setting for each VM using Hyper-V Manager one at a time… but there’s an easier way!
I can generate a report of all the MAC addresses for virtual machine in my environment by creating a list of my Hyper-V hosts (in a text file called Serverlist.txt) and typing the following command line:

get-vmnic -server (get-content .\serverlist.txt) | select * | out-gridview

Being able to create reports like this on the fly is super useful, and I can’t imagine how I would be able to gather information as easily with out the library (other than using System Center Virtual Machine Manager!).

The truth is, I’m not a scripting or a PowerShell expert.  I don’t use it every day and often forget how to use the library.  I’ve used Out-Gridview to create a report of all the functions in  the library that I keep in Excel using the following command line:

get-command -module HyperV | get-help | select-object -property name, synopsis | out-gridview

Simplified Management

f1044I perform all sorts of management task too, like creating new VMs or starting /saving the state of all VMs on a host – those things are a piece of cake to automate.  One super useful thing I  do with the library is to Live Migrate all the VMs from cluster node to another with one simple command line.  Say for example I wanted to move all the VMs from one node (HPDL380T) to another (HPDL380B), I could just use the following:

Get-VM -server HPDL380T | move-vm -Destination HPDL380B

These are just a few examples of how I use the library (lots more in the update to our book!).  If you would like to see examples of other ways to automate tasks with the library, leave me a comment.  For other great ideas on automation, checkout the TechNet Script Center.  It’s packed full of scripting examples, tutorials, witty repartee – all at the same low low price!

-John