Getting Started with PowerCLI: Start and Shutdown Virtual Machines

Posted on September 24, 2013 by Jeff Hicks in VMware with 0 Comments

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.

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.

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.

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

Here’s my result.

vSphere PowerCLI: Start and Shutdown VMs: Get-VM

Sponsored

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

vSphere PowerCLI: Start and Shutdown VMs: Get-VM

Once I know the properties I want, creating inventory reports is pretty easy.

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

Sponsored

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.

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.

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.

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.

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.

By default you will be prompted. But you can set the confirm parameter to 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.

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

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.

Sponsored

Tagged with