Hey, Scripting Guy! Virtual Machine Manager Cmdlets and Windows PowerShell
About the author: Susan Hill writes content for Virtual Machine Manager. While her early influences include Kernighan and Ritchie, more recently she has broadened her skills under the Windows PowerShell movement. She aspires to be the Gauguin of Windows PowerShell, and in preparation for this, she spends her spare time travelling to warm climates, discovering new wines, and painting.
After having a chat with the Microsoft Scripting Guy Ed Wilson about writing a guest blog entry, I was definitely considering my life a little boring compared to the adventures he recounts in his posts. I therefore decided to get myself a virtual life, and promptly looked up pictures of Fiji and other assorted tropical locales. Living in the Seattle area, I can’t help but dream of sunny places and palm trees when the dreary gray skies start to get on my nerves. Although the other Saturday it was a balmy 58 degrees, making me dig out my shorts and flip-flops, the weather promptly turned and we had snow flurries on the following Monday. Go figure.
Speaking of a virtual life, I spend my days working in the virtual world of System Center Virtual Machine Manager (VMM) and would like to take a few minutes to introduce you to some concepts you’ll need in order to write Windows PowerShell scripts for VMM. This is part one of a four-part series of VMM posts. This post is written for beginners working with VMM cmdlets, and starts with the basics. Over the next few days, we’ll move virtual machines between hosts and the library, create new virtual machines, and expire virtual machines created for self-service users.
VMM is built on Windows PowerShell. This means that all of the administrative tasks you can do through the Administrator Console you can also do using the VMM cmdlets provided with the Windows PowerShell – Virtual Machine Manager command shell.
When you start the VMM command shell, before using any of the cmdlets to access your managed environment, you first need to connect to your VMM server. If you start the VMM command shell from the UI, you are automatically connected to the VMM server. However, if you start the VMM command shell from the Start menu, you will need to connect to the server manually. In fact, all of the scripts contained in our VMM Scripting Guide begin with connecting to the VMM server:
Get-VMMServer –ComputerName “VMMServer01.Contoso.com”
If you chose to run the VMM cmdlets from the Windows PowerShell command window instead of the VMM command shell, you will need to add the VMM snap-in:
You can now see all of the VMM commands available to you by typing the following command:
Get-Command –CommandType cmdlet –PSSnapin Microsoft.SystemCenter.VirtualMachineManager
However, as a colleague pointed out to me the other day, he knows how to see the available commands and how to use the Get-Help cmdlet to view the help for the commands, but he wants to know where to start. So, here are a few commands that will help you get oriented in your VMM environment.
We’ll start by taking a look at the hosts you are currently managing with VMM. After you connect to the VMM server, type the following:
This returns all hosts managed by VMM, whether they are Hyper-V hosts, VMWare ESX Server hosts, or Virtual Server hosts. However, if you’re managing several hosts, you will see a lot of information scrolling through your Command Prompt window. If you’d like to see just the names of the servers managed by VMM, use the following command:
Get-VMHost | Format-Table name
Now let’s take a look at just one host, and change its properties:
# Get the host and store it in the variable $VMHost
$VMHost = Get-VMHost -ComputerName “VMHost01.Contoso.com”
# Set the host status as unavailable for placement
Set-VMHost -VMHost $VMHost -AvailableForPlacement $FALSE
You can return the host status to Available for placement by using the following command:
Set-VMHost -VMHost $VMHost -AvailableForPlacement $TRUE
Now, let’s take a look at the virtual machines on those hosts. You can get all of the virtual machines in your VMM environment by using the Get-VM cmdlet:
Get-VM | Format-Table name, status
Of course, now that you know the names of your hosts, you can get just the virtual machines for a particular host:
Get-VM –VMHost “VMMHost01.Contoso.com” | Format-Table name, status
Building on that concept, we can now manipulate individual virtual machines. For example, we can stop and start a virtual machine:
Stop-VM -VM “VM01”
Start-VM -VM “VM01”
You can also change the properties of a virtual machine:
# Get the virtual machine
$VM = Get-VM -Name VM01 -VMHost “VMMHost01.Contoso.com”
# Stop the virtual machine
Stop-VM -VM $VM
# Change the memory to 1024 MB
Set-VM -VM $VM -MemoryMB 1024
# Start the virtual machine
Start-VM -VM $VM
I hope this spurs your interest in the world of VMM and what you can do using the VMM command shell. Tomorrow, we’ll start building scripts that move around your virtual machines.