Getting Started with PowerCLI: Start and Shutdown Virtual Machines

Over the last few articles I’ve been introducing VMware’s vSphere PowerCLI. This is a PowerShell snapin that you can use to manage your VMware infrastructure. If you’ve missed the earlier articles you might want to quickly get caught up: In part one, we took a look at downloading and installing PowerCLI. In part two, we went through the steps of setting up and configuring PowerCLI. Next time, in part four I’ll go over PowerCLI and PSDrives, and in part five I created a new VM. Finally, in part six learn to use PowerCLI to manage your ISO files.

Previously I connected to my VMware server.

​ PS C:\> add-pssnapin vmware.vimautomation.core
PS C:\> connect-viserver esx.jdhitsolutions.local

Now I want to work with the virtual machines. Today I’ll show you how to retrieve virtual machines and how to start and shutdown VMs.

vSphere PowerCLI: Get Virtual Machines

The command to retrieve the virtual machines is pretty straightforward.

​ PS C:\> Get-VM
Name                 PowerState Num CPUs MemoryGB
----                 ---------- -------- --------
Globomantics HyperV  PoweredOff 1        4.000
ResearchDC           PoweredOff 2        2.000
Globomantics Mail    PoweredOff 2        3.000
Globomantics Web     PoweredOff 1        2.000
Win7                 PoweredOff 1        0.500
MyCompany XP         PoweredOff 1        0.375
Globomantics DB      PoweredOff 2        1.500
R2 Server Core       PoweredOff 1        1.000
Win7 Baseline        PoweredOff 1        0.500
MyCompanyDC          PoweredOff 2        0.820

One thing to be aware of is that the Hyper-V PowerShell module also has a Get-VM cmdlet. But I don’t worry about a name collision because I run PowerCLI in a PowerShell v2 session. But this may only apply to a subset of readers.
By default the cmdlet will return all virtual machines. But you can get virtual machines by name, server, or datastore, to name a few common approaches. Don’t know what your datastores are? Ask PowerShell.

​ PS C:\> get-datastore

Once you know the name, you can use it with Get-VM.

​ PS C:\> get-vm -datastore datastore1

Here’s my result.
vSphere PowerCLI: Start and Shutdown VMs: Get-VM

Of course there’s more to the virtual machine object, so ask PowerShell to see all of its properties.

​ PS C:\> get-vm MyCompanyDC | select *

vSphere PowerCLI: Start and Shutdown VMs: Get-VM
Once I know the properties I want, creating inventory reports is pretty easy.

​ PS C:\> get-vm | Sort MemoryMB | select Name,MemoryMB,NumCPU,Version,Host | format-table –AutoSize

As you see below in Figure 3, I’m using a simple table. But you could just as easily export to a CSV file or create a fancy HTML report.
vSphere PowerCLI: Start and Shutdown VMs: inventory report

Start-Virtual Machines

Time to get some work done, so I need to fire up some virtual machines. I bet you can guess the cmdlet name. You can start a single VM, multiples, or pipe names to Start-VM.

​ PS C:\> start-vm MyCompanyDC,ResearchDC

In this case the machines start pretty quickly and I get my prompt back. But if you are starting a number of machines, you might want to run the command as a VMware task. The cmdlet lacks an AsJob parameter, but you can start a virtual machine asynchronously, which has the same effect except the command is running as a background task on the VMware server.

​ PS C:\> start-vm "r2 server core" –RunAsync

Guest OS

Once the virtual machines are up and running, you can get some useful information from the guest operating system using the Get-VMGuest cmdlet.

​ PS C:\> Get-VMGuest -vm MyCompanyDC
State          IPAddress            OSFullName
-----          ---------            ----------
Running        {172.16.10.70}       Microsoft Windows Server 2003 Standar...

I love this – not only do I get the IP Address but I also get the operating system. With a little creativity, I can drill down and even get the hostname.

​ PS C:\> get-vm | where {$_.powerstate -match "On"} | Get-VMGuest | Select VMName,@{Name="Hostname";Expression={$_.ExtensionData.HostName}},@{Name="IP";Expression={$_.extensionData.IPAddress}},OSFullName | format-table –auto

This command is getting all powered on virtual machines and then the guest OS for each. The hostname can be found in the ExtensionData property. And even though there is an IPAddress property already, it is displayed as an array. The IP address in the extension data should be the guest’s primary IP v4 address. Although none of my VMs are multi-homed so this could still come back as an array.
Here are my results:
vSphere PowerCLI: Start and Shutdown VMs: Get-VMGuest

Shutdown Virtual Machines

To shut down a virtual machine, I prefer to do it from inside the guest operating system.

​ PS C:\> shutdown-vmguest win7

By default you will be prompted. But you can set the confirm parameter to false.

​ PS C:\> shutdown-vmguest win7 -Confirm:$false

The shutdown command is sent to the guest and you immediately get your prompt back. Be aware that in order for this to work, the guest OS must be running VMware Tools. By the way, you can also restart the guest.

​ PS C:\> get-vmguest researchdc | restart-vmguest

It is also possible to stop the virtual machine as it is running on the VMware host.

​ PS C:\> get-vm "r2 server core" | Stop-VM

However, this doesn’t initiate a clean shutdown. It simply terminates the running virtual machine, so you should use this only if the guest or VM is totally unresponsive.
Working with virtual machines and virtual guests can be a lot of fun. But know that if you don’t have VMware Tools installed on the guest, you will be limited with what you can accomplish.