Your basic ITPro blog... What's going on at work, what I'm interested in.

Monday, January 5, 2009

First work with Virtual Machine Manager and PowerShell

To date, I have done ALL of my VMM management in the GUI. I have not used PowerShell at all (please don’t yell at me). Well, that changed today.

I got asked by my boss if two VMs were on the same host or not. I didn’t remember off the top of my head, so I opened the VMM Admin Console to check. So much clicking… and waiting…

I decided to see if I could get at this information via PowerShell. I mean, I figured I could, as I knew that VMM was PoSH-friendly. I also decided that I would use this opportunity to explore V2 CTP3 a bit and write one of those fancy, advanced functions. And, rather than dot-sourcing a .ps1 file (from within my profile) to get the function into my environment, I made it a module.

I just installed V2 CTP3, so I am brand new to these cool new features. But, it doesn’t seem too complicated.

Anyway, here is the function that checks a VMM servers for managed VMHost boxes and lists the VMs on each host. First, though, this information can easily be had with the following command:

Get-VM VMMServer COMP01 | select VMHost, Name

But, I wanted to play, so I wrote:

####################################
# FUNCTION: Get-VMEnvironment
#
# WRITTEN BY: Derek Mangrum
#
# 2009-01-05 : Initial Version
####################################
function Get-VMEnvironment
{
<#
.SYNOPSIS 
    Reports on VMM-managed environment
.DESCRIPTION 
    Lists VMs by VMHost
.NOTES 
    Author     : Derek Mangrum
    Requires   : PowerShell V2 CTP3 or later
    Requires   : Ping-Computer function
.LINK 
    http://grinding-it-out.blogspot.com/
.EXAMPLE 
    PS> Get-VMEnvironment
.EXAMPLE
    PS> "server1","server2" | Get-VMEnvironment
.PARAMETER VMMServer
    Name of VMM Server - DEFAULT: COMP01
#>
    
    param 
    ( 
        [Parameter(Position=0, Mandatory=$false, ValueFromPipeLine=$true)]
            [string]$VMMServer = "COMP01"
    )
    
    BEGIN
    {
        Write-Host
    }
    
    PROCESS
    {
        if (Ping-Computer $VMMServer) 
        {
            if (Get-WmiObject win32_service -computerName $VMMServer | Where-Object {$_.name -match "VMMService"}) 
            {
                Get-VMMServer $VMMServer | Out-Null
            
                Get-VMHost | ForEach-Object {
                    Write-Host "Report for VM Host: $($_.Name.Split(".")[0].ToUpper())";
                    Get-VM -VMHost $_ | 
                    Select-Object Name, StatusString | 
                    Format-Table
                } #END Get-VMHost...
            } #END if (gwmi...)
            else
            {
                Write-Host "$VMMServer is not a VMM Server.`n" -foregroundcolor Red
            } #END else
        } #END if (Ping-Computer $VMMServer)
        else
        {
            Write-Host "$VMMServer is not available.`n" -foregroundcolor Red
        } #END else
    }
}

Export-ModuleMember Get-VMEnvironment

Overkill? Probably… But fun!

Additional Info

My photo
email: support (AT) mangrumtech (DOT) com
mobile: 480-270-4332