The Connect-VM Cmdlet

Just realized that Tome did this a week ago. Oh well….I guess here is my version for reference. I like how we even named our cmdlets the same :D

http://powertoe.wordpress.com/2012/03/13/powerbits-8-opening-a-hyper-v-console-from-powershell/

I’ve been playing with Windows Server 8 Hyper-V cmdlets a lot over the last week and one thing has bugged me. There is no cmdlet to connect to a VM using the VMConnect application. VMConnect accepts really simple arguments so that it is actually pretty easy to use on the command line. This was not entirely obvious to me though. Had to do at least a little bit of digging.   Here is the help dialog from VMConnect.

It’s pretty simple. I felt that this should be a cmdlet though! It would be nice to pipe Virtual Machine objects to a cmdlet and have the window pop open. I don’t want to have to go into Hyper-V Manager just to pop open the viewer when I’m doing pretty much everything else in the command line. I wrote a simple little advanced function to take care of this for me.

function Connect-VM
{
    [CmdletBinding()]
    param([Parameter(ValueFromPipeline=$true)][Microsoft.HyperV.PowerShell.VirtualMachine]$VM,
    [Parameter()][string]$VMName,
    [Parameter()]$Server="localhost")

    process {
        if ($VM -ne $null)
        {
            $VMName = $VM.Name
        }

    if ([String]::IsNullOrEmpty($VMName))
    {
        Get-VM | Connect-VM -Server $Server
    }
    else
    {
        Start-Process VmConnect -ArgumentList $server,$VMName
    }
  }
}

All the cmdlet does is pop open the VMConnect application and display the VM that was piped to it. Saves me at least a little bit of time. If no arguments are specified it will open a VMConnect instance for each one of the VMs on the local server. It can also do the same for remote servers.

You can leave a response, or trackback from your own site.

One Response to “The Connect-VM Cmdlet”

Leave a Reply


+ 7 = nine

Subscribe to RSS Feed Follow me on Twitter!