Use PowerPacks to Learn PowerShell

The Hyper-V PowerPack for PowerGUI that I published last month is one of my favorite PowerPacks so far.  It’s a great example of how you can take a task that is pretty complicated for someone who doesn’t program for a living (like using PowerShell to manage Hyper-V via WMI), simplify it with rich PowerShell functions that look and feel like cmdlets, and then build a user interface on top those functions to perform management and automation tasks.  Since all of the PowerShell script behind the PowerPack is freely available through the PowerGUI Admin Console, using a PowerPack like the Hyper-V PowerPack is a great way to learn PowerShell because it allows you to get familiar with how specific administrative tasks translate into PowerShell scripts that you can then use for automation, provisioning, scheduled tasks, etc.

The scripts powering the Hyper-V PowerPack are particularly interesting because there are no cmdlets available yet to manage Hyper-V unless you use Microsoft System Center Virtual Machine Manager 2008.  The prescribed way to manage Hyper-V via script with the release of Windows Server 2008 is WMI.  Since working with WMI directly is not much fun after you have become spoiled with the ease-of-use you get with PowerShell cmdlets, not to mention quite difficult, I have included a lot of useful functions (over 30 of them so far) that wrap the WMI management code inside of a cmdlet-like experience complete with support for pipelining so that you can write scripts to work with your Hyper-V servers much more easily.  Here’s a complete list of the functions that are included in the current revision of the Hyper-V PowerPack:

Job Management

Get-HyperVJob
Wait-HyperVJob

Server Configuration

Get-HyperVServerSettings
Set-HyperVServerSettings

Service Management

Get-HyperVService

Physical Network Adapter Management

Get-HyperVPhysicalNIC
Set-HyperVPhysicalNIC

Virtual Network Management

Get-HyperVVirtualNetwork
New-HyperVVirtualNetwork
Remove-HyperVVirtualNetwork
Rename-HyperVVirtualNetwork
Set-HyperVVirtualNetwork

Virtual Machine Management

Checkpoint-HyperVVirtualMachine
Export-HyperVVirtualMachine
Get-HyperVVirtualMachine
Import-HyperVVirtualMachine
New-HyperVVirtualMachine
Remove-HyperVVirtualMachine
Rename-HyperVVirtualMachine
Restore-HyperVVirtualMachine
Set-HyperVVirtualMachine

Virtual Network Adapter Management

Add-HyperVVirtualNIC
Get-HyperVVirtualNIC
Remove-HyperVVirtualNIC
Set-HyperVVirtualNIC

Virtual Ide Drive Management

Add-HyperVVirtualIdeDrive

Virtual Scsi Drive Management

Add-HyperVVirtualScsiDrive

Snapshot Management

Get-HyperVSnapshot
Set-HyperVSnapshot
Remove-HyperVSnapshot
Rename-HyperVSnapshot
Update-HyperVSnapshot

Virtual Hard Disk Management

Get-HyperVVirtualHardDisk
New-HyperVVirtualHardDisk

These functions do not provide comprehensive coverage of all Hyper-V features and there are many more that I plan to add in a future update, but it’s well on it’s way to becoming a comprehensive set of functions.  If you are looking for specific functionality that doesn’t appear to be available through these functions, I encourage you to take a look at the methods on the rich objects that are output by these functions.  There is a whole lot more functionality available than you might think!  I just haven’t had time to expose all of the method-based functionality as cmdlet-like functions yet.

To use these functions in your own scripts you simply have to copy them out of the Hyper-V PowerPack.  Be sure to keep an eye on dependencies and make sure you get all the functions you need.  Most of the PowerPacks I’ve been working on lately come with useful functions like this, for the explicit reason that I want people to be able to use them inside and outside of PowerGUI.  I’ll blog about them as time permits, but until I make time for that I wanted to at least make you aware that they are there as a resource and as a learning tool.

If you want to write PowerShell scripts to manage your Hyper-V servers, doing yourself a favor to take a look at the Hyper-V PowerPack and the functions that it contains.  They might go a long way to helping you get your work done faster and more easily, either by using the functions themselves in your scripts or by looking at the PowerShell code behind the functions and learning how to get WMI to do what you want to do.

Kirk out.

Share this post:

Hyper-V PowerPack Updated

Since the Hyper-V PowerPack for PowerGUI was published several weeks ago, I have found and fixed several defects, particularly in the area of adding and removing managed Hyper-V servers.  I have also enhanced the search functionality so that it is easier to use when you work with one domain in particular.  To see the complete list of changes, see the revision history section on the Hyper-V PowerPack page.

If you downloaded the Hyper-V PowerPack before Friday, November 14, 2008, I recommend that you visit the Hyper-V PowerPack site and upgrade your PowerPack to the most recent version.  Upgrading the Hyper-V PowerPack can be done in a few easy steps, as follows:

  1. Download the new version of the PowerPack.
  2. Open the PowerGUI Admin Console if you haven’t already.
  3. Right-click the Hyper-V folder at the root of the Hyper-V PowerPack in PowerGUI and click on Delete.
  4. Right-click the root node in the PowerGUI tree, click on Import, and select the updated HyperV.powerpack file that you downloaded.

Once you have done these steps, you will be using the most recent version of this PowerPack.  If you have any feedback or questions you would like to raise for this PowerPack or any others, please feel free to contact me directly (see my about page for details) or leave me a note on the PowerGUI Forums.

Enjoy!

Kirk out.

Share this post:

PowerGUI: Now with 100% more Hyper-V!

About three months ago I decided to go on a deep technology dive, exploring a facet of some technology with PowerShell and seeing what made it tick.  Typically these sorts of things take anywhere from a few days to a few weeks to work out the details and the end result is some useful scripts that I then blog about and try to leverage with a PowerPack or two.  Well this time I ended up going a little deeper than I have in the past, and I decided to go silent on my blog while I worked out the PowerShell scripts behind this huge project.  Today I’m happy to end that silence by sharing with you the largest PowerPack that has been published to date!

Hyper-V DrinkAs of today, PowerGUI users can download a free Hyper-V PowerPack for PowerGUI!  This PowerPack has been a huge undertaking, to put it mildly, and I’m absolutely thrilled with the end result (and so happy that I’ve finally finished the first version…whew)!  Here’s a list of some of the key features that this PowerPack provides:

  1. Bulk management of multiple VMs, Virtual Networks, Virtual Hard Drives, Snapshots, etc. within one Hyper-V servers and across multiple Hyper-V servers.
  2. Support for remote management of Hyper-V servers using alternate credentials.
  3. Integrated management of standard Windows features like Processes, Services, Event Logs, etc.
  4. Automatic credential caching (in memory, not to disk) allowing you to enter a password for a Hyper-V server you are managing only once per PowerGUI session.
  5. Discovery of Hyper-V servers through Active Directory.
  6. Over 30 rich user-defined functions that wrap the Hyper-V WMI interfaces and provide a cmdlet-like experience when scripting with Hyper-V while outputting rich, custom objects complete with properties and methods to script to your heart’s content.  Many of these functions support CSV input through Import-Csv, so there are a lot of opportunities for provisioning already available.
  7. 101 links and actions allowing you to manage just about everything you would want to manage in Hyper-V, including configuration of security via AzMan Scopes, Snapshot refresh, and many, many more.

In terms of Hyper-V functionality, the PowerPack is pretty comprehensive at this point.  There are some features that I have support for in functions that I just didn’t have time to put into the PowerPack at the end of this cycle.  There are also quite a few features in the PowerPack that aren’t available in the Hyper-V Manager.  But that just means there are opportunities for new features to be added in later versions of this PowerPack.  Let me know what features are the most important to you so that I can help you meet your Hyper-V management/scripting needs.

To give you an idea of what it took to make this PowerPack, the main script that contains the function library I’m using in this PowerPack is over 9500 lines long (if you’re new to PowerShell, that’s a heck of a lot of code in a language that lets you do so much with very, very short scripts)!  This huge script, and the scripts powering the links and actions, plus the rest of the PowerShell script behind the PowerGUI PowerPacks are all visible to the end user.  If you’re trying to figure out how to do something with Hyper-V via WMI and you can’t find the answer, take a look at the functions I have and see if they are already doing what you want to do.  Then you can use the functions or try to copy out the code that you need and go from there!

Just a quick note to get you started, once you download and import the PowerPack into PowerGUI, expand the Hyper-V node and then click on Managed Hyper-V Servers.  That will expose the Add Connection functionality you need to use to add your first connection to a Hyper-V server.  Once you’ve done that, you’ll be able to use the rest of the functionality against that server.

If you have any feedback for this PowerPack (or any others), feel free to leave a note on my blog or on the PowerGUI discussion forums.  We’ll be sure to try and help you meet your needs any way that we can!

So what are you waiting for?  Go to the Hyper-V PowerPack site and download the PowerPack today!

Kirk out.

Share this:

Essential PowerShell: Name your custom object types

PowerShell is a very flexible scripting language that allows users to dynamically create and/or extend objects with additional methods and properties.  This is very useful when you’re trying to build up a rich data set with all of the properties or methods you need.  One important thing that is often overlooked when people are writing scripts that do this is that they can also give those objects a type name.  Why is this type name important?  Three reasons:

  1. If you want to further extend those types automatically via a type data file, you’ll want a unique type name so that only the appropriate objects are extended.
  2. If you want to apply specific default custom formatting to those types via a format data file, you’ll want a unique name so that only the appropriate objects are formatted this way.
  3. If you want to associate specific links and actions with your custom object type in PowerGUI, you’ll want a unique name so that you don’t get links and actions associated with other types.

In practice there are only two use cases where I need to create a custom object type name, and I apply different names depending on the scenario I’m working with at the time.

If I have created a brand new generic PSObject, then I apply a name appropriate to the object.  In this case, after I created my custom object and added the properties, I would do the following:

$myObject.PSObject.TypeNames.Insert(0,’MyObjectTypeName’)

Alternatively, if I am extending an object of a particular type, then I apply an extended type name for that object to the modified version.  In this case, after I created my custom object and added the properties, I would do the following:

$derivedTypeName = $myObject.PSObject.TypeNames[0]
$myObject.PSObject.TypeNames.Insert(0,”$derivedTypeName#MyExtensionName”)

Then if you create type or format data files, you simply need to use your new type name in appropriate XML attribute to set up the association.  Or if you’re adding functionality to PowerGUI, any links and actions you create will automatically be associated with the lowest derived object type, which will be the type name you applied to the object before outputting it in the PowerGUI data grid.

You can add as many type names as you want to your objects, so if you want to create a virtual object hierarchy with your custom object types, and then associate format or type data specifications with derived or base object types, you can do that as well through multiple calls to the Insert method on the TypeNames collection.  This can be useful if you want to share a certain set of functionality between two types of objects you are creating but in addition you want some functionality to be specific to each type and you don’t want to duplicate code.

Hopefully this will encourage you to name your custom object types and define their default properties in scripts that you share with others.

If you’re working with custom objects you should also check out the follow-up post to this that resulted from Hal’s comments on this post, titled “Essential PowerShell: Define default properties for custom objects“.

Enjoy!

Kirk out.

Share this post:

PowerGUI hits 100,000 downloads!

Last week marked a major milestone in the history of PowerGUI.  Since the first beta was released to the web last year, PowerGUI has been downloaded 100,000 times!  Thanks to everyone for their support and participation in the forums!

Still not using PowerGUI as part of your PowerShell toolbelt?  Download the latest version from www.powergui.org and see what it’s all about!

Kirk out.

Share this post:

PowerPack Challenge now open!

I mentioned earlier that there is a PowerGUI contest going on this summer called the PowerPack Challenge.  Well as of July 1st (Happy Canada Day everyone!), this contest is officially open and participants can start submitting their entries!

Interested in the details?  Read all about it here!

I look forward to seeing your contest entries!

Kirk out.

Share this post:

PowerGUI wins Best of Tech Ed 2008 IT Professional Breakthrough Product award!

Well color me a happy camper!  Windows IT Pro just announced that PowerGUI has won the Best of Tech Ed 2008 IT Professional Breakthrough Product award!  Breakthrough Product is described by Windows IT Pro as the best single product of the Tech Ed 2008 IT Professionals conference, and could be from any IT Pro award sub-category.  Do yourself a favor and download PowerGUI today!  Even better, use PowerGUI to enter the PowerPack Challenge contest!

Kirk out.

Share this post:

Why isn’t my PowerPack showing up in PowerGUI?

Here’s an interesting question that came up recently:

“I just installed PowerGUI with the Exchange PowerPack and then I installed the Exchange 2007 Management Console.  The Exchange folder isn’t showing up in the PowerGUI Console.  Can you help?”

Yes, I can help.  Actually I came across this very same scenario myself not too long ago when working with a new VM.

If you install PowerGUI today, you can let it pick which of the core PowerPacks to install.  By default, PowerGUI will pick the PowerPacks for which you have the required PowerShell snapin(s) already installed.  For example, this means that the Exchange 2007 PowerPack will be installed automatically if you have the Exchange Management Console installed on your local machine, and the Active Directory PowerPack will be installed automatically if you have the Quest AD Cmdlets installed on your local machine.

Alternatively, you can also pick which PowerPacks you want to install yourself whether you have the required snapin(s) or not by using the custom install option.  I usually take this approach when I install PowerGUI, picking all PowerPacks so that I get all of the functionality that it comes with.  Most of the time this works fine when I have already installed all of the snapins I need, but what happens if you are missing one or more of the required snapins?  When you open PowerGUI for the first time your PowerGUI tree will automatically load all of the PowerPacks you installed for which you have the required snapins.  If you installed PowerPacks for which you don’t have the required snapins, they won’t show up in the PowerGUI tree.

Sounds like an easy enough problem to solve, right?  You can just install the missing snapin and then come back into PowerGUI and…um…notice that the PowerPack isn’t there.  This is because the automatic loading of the PowerGUI tree is only done once (at the moment; this will likely change in the future).  If you don’t have the required snapin installed when that happens, the PowerPack doesn’t get loaded into the PowerGUI tree.  Here lies the problem for the individual who asked the question stated earlier.

Fortunately the solution is straightforward once you know where the missing PowerPack is stored.  When PowerGUI is installed, all PowerPacks that are installed with it are placed in the PowerPacks subfolder of the PowerGUI installation folder.  To load a PowerPack that is missing because the prerequisite snapin wasn’t installed earlier, all you need to do is select File | PowerPack Management, click Import, browse to that folder and select the PowerPack that you want to import.  Once PowerGUI has verified that you have the required snapin(s) for that PowerPack installed it will import the PowerPack and your missing folder will be available in the PowerGUI tree.  And in case you weren’t aware, this is also the same way you would extend PowerGUI with other PowerPacks that you download from the library…download the PowerPack file, right-click the root node, select Import, and then select the PowerPack file you downloaded.

Hopefully this will help as you start using more snapins to manage and automate more with PowerShell and PowerGUI.

Kirk out.

Share this post:

Interview with Dmitry Sotnikov about PowerShell and PowerGUI on IT TV

Windows IT Pro magazine recently launched their new IT TV website, a community site that hosts videos relevant to today’s IT professional.  One video that caught my interest today is an interview with Dmitry Sotnikov discussing PowerShell and PowerGUI.  Curious about what PowerGUI is and how it might help you?  Check out the interview to learn more about it!

Kirk out.

Share this post:

Enter the PowerPack Challenge!

Ok, I’m really excited about this one.  Quest Software (my employer and sponsor of all PowerGUI development) has created a PowerGUI contest!  Whether you are a newcomer to PowerShell, a seasoned PowerShell scripter or somewhere in between, why not experiment with all of the creative things you can do with PowerGUI and PowerShell and enter the PowerPack Challenge!  All you need to do to enter is create and/or update one or more PowerPacks during the contest period (between July 1, 2008 and September 30, 2008).  There are some great prizes to be won, as described on the PowerPack Challenge contest page!

So now that you know about the contest, what should you do next?  Since the contest period doesn’t start until July 1, 2008, you have some time to prepare yourself for the contest.  if you aren’t totally familiar with PowerPacks or how to create them, you can learn how to do that by watching the flash demo I created here.  If you want to browse existing PowerPacks to get an idea what is already available, you can look in the PowerPack library here.  Or if you are attending the IT Pro week of TechEd 2008, there are a lot of sessions, hands on labs, instructor led labs, and more with PowerShell content and you can learn more about PowerShell and PowerGUI by visiting the demo station in booth 1101.

And please, just like anything else I post on this blog, if you have questions don’t hesitate to ask.

Thanks and good luck with your contest entries!

Kirk out.

Share this post: