Prof. Powershell
Manage VMs with PowerShell
Manage the whole VMware infrastructure from PowerShell. Yes, you heard right; it can be done.
- By Jeffery Hicks
- 05/02/2011
Let's continue our look into what other great things you can do once you have a little PowerShell under your belt. If you run even a single VMware server you are probably familiar with managing it with the vSphere client.
But did you know that you can manage your entire VMware infrastructure from PowerShell? This is crazy cool stuff. You'll need to go here and download the most current version of PowerCLI. Once installed you can either launch it from the Start Menu or manually load the snapin into your existing PowerShell session:
PS C:\> Add-PSSnapin VMware.VimAutomation.Core
As we've been doing, use Get-Command to see what cmdlets await you:
PS C:\> Get-Command -Module VMware.VimAutomation.Core
Before we can do anything we need to connect to a server. This can be either an ESX host or a vCenter server:
PS C:\> Connect-VIServer ESX -User Root -Password "P@ssw0rd1"
But once connected we get some handy PSDrives for navigating our VMware virtual infrastructure:
PS C:\> dir vi:\ha-datacenter\vm
Name Type Id
---- ---- --
MyCompanyExch2003 VirtualMachine VirtualMachi...
Win2K8R2 VirtualMachine VirtualMachi...
R2 Server Core 1 VirtualMachine VirtualMachi...
MyCompanyDC 2K3R2 VirtualMachine VirtualMachi...
MyCompany Win7 VirtualMachine VirtualMachi...
Win2K R2 Baseline VirtualMachine VirtualMachi...
ResearchDC VirtualMachine VirtualMachi...
Cluster Alpha VirtualMachine VirtualMachi...
Cluster Bravo VirtualMachine VirtualMachi...
MyCompany Windows 2008 VirtualMachine VirtualMachi...
MyCompany Exchange 2007 VirtualMachine VirtualMachi...
MyCompany XP VirtualMachine VirtualMachi...
MyCompany Vista VirtualMachine VirtualMachi...
Starting a virtual machine is as simple as this:
PS C:\> start-vm "myCompanyDC 2k3r2" -RunAsync
I used the -RunAsync parameter to create a background VMware task so that I don't have to wait for it to complete. The Get-Task cmdlet will provide information about all my tasks.
Getting a list of running virtual machines leverages the PowerShell you already know:
PS C:\> Get-VM | Where {$_.PowerState -match "On"}
Name PowerState Num CPUs Memory (MB)
---- ---------- -------- -----------
MyCompanyExch2003 PoweredOn 1 384
MyCompanyDC 2K3R2 PoweredOn 2 840
ResearchDC PoweredOn 2 2048
PowerCLI also lets you peek into the guest operating systems:
PS C:\> Get-VM | Where {$_.PowerState -match "On"} | Get-VMGuest | Format-Table VM,IPAddress,OSFullName -auto
VM IPAddress OSFullName
-- --------- ----------
MyCompanyExch2003 {172.16.10.71} Microsoft Windows Server
2003, Standard Edition (32...
MyCompanyDC 2K3R2 {172.16.10.70} Microsoft Windows Server
2003, Standard Edition (64...
ResearchDC {172.16.10.80} Microsoft Windows Server
2008 R2 (64-bit)
This is merely a taste. There's much more you can do with VMs and guests including modifying configurations, working with snapshots and upgrading VMTools.
Need to pause or shutdown a guest? This couldn't be easier:
PS C:\> get-vm "MyCompanyExch2003" | Suspend-VMGuest
State IPAddress OSFullName
----- --------- ----------
Standby {172.16.10.71} Microsoft Windows Server 2003,
Standard Edi...
To restart use Start-VM.
PS C:\> get-vm "MyCompanyExch2003" | Start-VM
Many of the cmdlets support -WhatIf and -Confirm so that you can be confident before making any sweeping changes such as shutting down all the running virtual machines:
PS C:\> Get-VM | Where {$_.PowerState -match "On"} | Shutdown-VMGuest -whatif
What if: Performing operation "Shutdown VM guest." on VM "MyCompanyDC 2K3R2".
What if: Performing operation "Shutdown VM guest." on VM "ResearchDC".
Depending on your licenses and servers, some cmdlets and features may not be fully functional. But even with the free ESXi platform, there's a lot you can accomplish from a PS prompt to warrant taking PowerCLI for a spin.
About the Author
Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.