VMGuru.com - Home of the Virtualization Gurus

Our Books REALLY are Available Now!

Don’t become overwhelmed by your VMware Infrastructure project. Use the books that tens of thousands of others have used to design, plan, and implement their virtual infrastructures.Download Now!

Getting Started with PowerShell and PowerGUI in your Virtual Infrastructure PDF Print
Written by Scott Herold   
Monday, 29 December 2008 08:44

I've been doing a lot of work with PowerGUI and PowerShell in VMware environments recently and I've finally started to show some of the fruits of my labor to various coworkers.  I've found that the individuals who already understand the flexibility that PowerShell provides to a VMware environment instantly have that light bulb kick on that says "Oh, that's awesome".  There are others who have not had the privilege of working with PowerShell and automating management tasks in their environment.  I find it takes a little bit of effort to show the value of this "new" technology to get them to see the value.  I wanted to take a moment today to provide a quick 5-10 minute primer to getting started with PowerGUI and PowerShell in a VMware Infrastructure. 

This is the first part of what will likely be a 3-part series on how to fully take advantage of PowerGUI and PowerShell to more effectively manage your Virtual Infrastructure.  I'm hoping parts 2 and 3 include some video walkthroughs, as that is really the only way to convey just how cool this technology really is, but that all depends on how accommodating my work schedule is.

Pre-Requisites

Before getting started with PowerGUI and PowerShell in your VMware Infrastructure, there are some pre-requisite installs that are required.

In addition, before getting too far, I want to disclose that I am a Quest Software (Developers of PowerGUI) employee.

Component Installation
All that is needed to get started with PowerGUI and PowerShell for VMware

  1. Download and install Microsoft PowerShell
  2. Download and install VMware Infrastructure Toolkit for Windows
  3. Download and install Quest Active Directory Cmdlets - Ties Windows and AD Management alongside Virtualization Management
  4. Download and install Quest PowerGUI
  5. Download and import Quest PowerGUI PowerPack for VMware
Installation is relatively straight forward for all components.  It is the traditional Next, Next, Finish type setup for all 5 components. 

Getting Starting with PowerGUI

After installing PowerGUI you may need to complete one or two extra steps to ensure the VMware PowerShell Cmdlets are properly registered.  First, once inside PowerGUI, Click on File -> PowerShell Libraries.

Second, ensure the Quest.ActiveRoles.ADManagement and VMware.VimAutomation.Core Snapins are enabled.

The final step in the PowerGUI setup is to import the PowerPack for VMware.  Simply Right-Click on the PowerGUI icon in the Tree View Pane and point to the VMware.VIToolkit.powerpack file that was downloaded in step 5.

Links vs. Actions

PowerGUI is based on the idea of links and actions.  I will get into more detail in Part 2 of this series, but PowerShell works by using a system of pipelines to pass return data from one command directly into another.  As an example of this, a user can execute the following two commands at the VI Toolkit CLI:

Connect-VIServer VirtualCenter_Server
Get-Cluster clustername | Get-VMHost

 

These two command would first connect and authenticate to the VirtualCenter specified by the "VirtualCenter_Server" retrieve the cluster specified by the "clustername" parameter and retrieve a list of the ESX Hosts that make up that cluster.  The process of passing the return from one command directly into another query is the basis of a link in PowerGUI.  Links are not limited in their length.  An example of this can be seen with the following code:

Connect-VIServer VirtualCenter_Server
Get-Cluster clustername | Get-VM | Get-Snapshot

 

This script will first retrieve the specified cluster, retrieve a list a VMs that are contained in the cluster, and finally provide a list of all snapshots of the cluster's VMs.  If you were to try and receive this same information using other means such as the VI Perl Toolkit or even worse, the actual VI SDK, you would find that this would take multiple lines of code, whereas with PowerShell, it was all done with a single line of code.  Efficient huh?  You can even extend this one step further by not only retrieving all open snapshots, but also performing an action of deleting them: (USE CAUTION WITH THE FOLLOWING SCRIPT, IT WILL REMOVE ALL SNAPSHOTS WITHOUT PROMPTING YOU)

Connect-VIServer VirtualCenter_Server
Get-Cluster clustername | Get-VM | Get-Snapshot | % {Remove-Snapshot $_ -Confirm:$false}

 

There's a lot going on in the above script.  As with the previous example, we are retrieving a list of all snapshots for VMs on the specified cluster name.  The last pipe actually states that for each snapshot (shortcut with "%") remove the passed snapshot (shortcut with $_) with a parameter that instructs PowerShell to not confirm the deletion of each one.  For those that ARE familiar with PowerShell, you may notice that this script could easily be performed without having to specify foreach (%).  I'm simply showing off some of the PowerGUI shortcuts here.

 

Actions in PowerGUI are just as the name implies.  It takes an object from a link and performs a set of actions.  The best part about actions in PowerGUI is that they can be applied to either a single returned object (ex. Get-Cluster clustername) or a group of objects (ex. Get-Cluster clustername | Get-VM).  This makes it very simple to perform an identical action across many ESX Servers.  Several examples from my consulting days immediately come to mind.

  1. Rescan all Storage HBAs on all hosts in a cluster after adding a new LUN
  2. Remove all snapshots from a host due to VCB leaving them open 
  3. Selectively update the VMware Tools on multiple VMs
  4. Add a new Virtual Switch or Port Group to all VMs in a cluster
  5. Detach the CDROM and Floppy drives of all VMs in a cluster so VMotion/DRS will work

The possibilities are nearly endless. 

Connecting to the VI with PowerGUI for the First Time

Before you have the ability to manage anything, you need to establish a connection with a VMware ESX Host or VirtualCenter instance.   To do this, simply expand the VMware Folder and select "Managed Hosts".  This will change the Links and Actions pane to only include the items relevant to a VMware Connection in PowerGUI.  To add your ESX Host or VirtualCenter simply click "Add Connection" and fill out the form that is displayed.  Please note that it is possible to provide a list of ESX Hosts or VirtualCenter Management Servers that share the same credentials to make bulk import signicantly simpler.

 

Once you have a connection established you are free to roam the PowerGUI interface through one of two different methods.  The first is through "Tree Navigation" in the left panel under the VMware folder.  This should feel similar to managing your environment with VirtualCenter.  The second method is by using the list of Links and Actions available in the right panel.  For many, this provides a quick way to view the exact object you are looking for in a quick fashion. 

Both methods have some distinct advantages and disadvantages, which I will highlight in Part 2 of this series later this week.  For now, feel free to roam the PowerGUI interface and start to experience many of the advantages that PowerGUI and PowerShell can enable in your VMware Infrastructure.

Additional Resources

For those looking to dig a little deeper, I'm providing the following links that I found invaluable when starting to play with PowerGUI and PowerShell.

Quest PowerGUI Website
VMware PowerShell Website
Eric Sloof's Virtualization blog
PowerShellCommunity.org
Dmitry Sotnikov's Blog
Kirk Munro's Blog
PoshCode.org

Trackback(0)
Comments (0)Add Comment
Write comment
You must be logged in to post a comment. Please register if you do not have an account yet.

busy
 

Buy on Amazon