Maksim Kabakou - Fotolia

Tip

How to use PowerCLI and how it can help you

PowerCLI enables you to use PowerShell commands to automate a VMware environment, streamlining your system and adding simplicity compared to using a GUI. Here's how to get started.

Although some people might think of PowerCLI as a different scripting language or alternative to PowerShell, PowerCLI is neither of these things. PowerCLI is a command-line interface tool for vSphere that "snaps into" the PowerShell framework. If you're wondering how to use PowerCLI, you can get started by learning and experimenting with simple commands.

PowerShell executes PowerCLI commands through the use of cmdlets -- or short scripts meant to perform a single function -- for High Availability and Distributed Resource Scheduler setup, infrastructure activity reporting and VM alerts. In short, it automates management for vSphere environments.

PowerCLI doesn't simply use the framework of PowerShell to execute PowerShell commands; it adds a vast number of additional commands. The initial release of PowerCLI included over 500 cmdlets, and both VMware and the community have since added even more.

The PowerShell framework

For the former Windows admin with PowerShell experience, using PowerCLI might feel like familiar territory. For the non-Windows admin or one that shied away from PowerShell initially, using PowerCLI might feel like you're starting from scratch with a new technology.

PowerShell commands are made up of two pieces: a function, which defines an action to take, and a cmdlet, which defines an object to perform that function on. The command Get-VMHost, for example, is a function of Get enacted on the VMHost object. You can then follow up a command with a parameter, such as -VM $ProductionVM1, which returns information on the host that your specified VM resides on.

You can use the search function in the VMware client to do the same thing the scripts do, but PowerCLI commands work faster. PowerCLI also enables you to save these scripts to get the information you need whenever you need it, without hunting and pecking in the GUI, which becomes a challenge especially when feature locations, right clicks and menus change. PowerCLI commands don't change just because the interface does, and you can use the same command an unlimited number of times.

PowerCLI vs. GUI

PowerCLI enables you to do all the functions you can do with the GUI but without the same number of steps. You can build up a personal PowerCLI library to create functions at scale without having to click through menu after menu.

For example, if you want to move all your VMs from one host to another, you must click and drag your VMs and take several steps in the GUI. By contrast, in PowerCLI, you can accomplish this with only one line of code:

Get-VMhost esxi-1 | Get-VM | Move-VM – Destination (Get-VMHost esx1-2)

When you compare PowerCLI to a GUI, you realize how much time adds up when you use the GUI method. You can easily modify your code to do things such as move VMs to different resource pools, folders or data stores by simply changing the final arguments of a cmdlet.

PowerCLI has a staggering number of commands; you could spend six months trying to learn every PowerCLI command and still not know them all.

PowerCLI also doesn't require you to create large amounts of code from scratch; you can simply alter existing code to suit your own needs. You can find a vast amount of resources from both the community and VMware itself with code examples, and you can build up your library of functions with both your own modified codes and those of others to simplify your daily administration life.

Get started with PowerCLI

If you know how to use PowerCLI and want to start using it for higher-level and large-scale administration, keep in mind that code executes immediately. That means, if you make a typo -- a simple enough mistake -- you can do a lot of damage before you even realize it.

Start with a few smaller, safer commands until you get the hang of how PowerCLI works. You can use PowerCLI to pull status reports instead of making broad, data center-wide changes. Once you connect your PowerCLI session, try running a simple Get-VM command to get a listing of your VMs and basic status information on them, such as power state or CPU and memory configuration.

You can also easily check the status of your snapshots with a single line:

Get-VM |Get-SnapShot | Select VM,Name

You can run this simple command at the start of the day to verify snapshots, which, when left unchecked, can cause a number of issues. You can even schedule it to run automatically at a certain time and push the output to a CSV file.

Incorporate PowerCLI into your everyday

PowerCLI has a staggering number of commands; you could spend six months trying to learn every PowerCLI command and still not know them all. You must consider how PowerCLI can work alongside your traditional administration tools and practices.

Start slow with PowerCLI. Use it where it makes the most sense, and don't try to force it everywhere immediately. Engage with the community sites, and try code examples to help you get started.

Dig Deeper on VMware ESXi, vSphere and vCenter

Virtual Desktop
Data Center
Cloud Computing
Close