Getting Started with VMware PowerCLI
With vSphere 5, the traditional ESX Server has gone away and the permanent replacement is ESXi Server. Of course, the big difference is that ESXi Server doesn’t have a service console. That service console was a special privileged virtual machine that was used for administration and scripting functions for an ESX host. ESXi Servers, with their tiny Busy-box Linux super-slim console isn’t going to work for scripting and isn’t efficient for mass administration & configuration of ESXi Servers.
VMware recommend one of two options with one being the vSphere Management Assistant (vMA). For information on vMA, see my video “Using the vSphere Management Assistant”. However, scripting in vMA will be done, very likely, using Perl. For most VMware Admins, Microsoft’s PowerShell is a more appealing option as it works for a wide range of Microsoft and third-party products. Thus, your knowledge of PowerShell would allow you to script Windows Server tasks, Exchange Server tasks, and more. Well, that “more” is also VMware vSphere server tasks. When you use PowerShell with VMware’s “cmdlets”, it’s called PowerCLI and that is the best option for mass vSphere administration and scripting.
What You Need to Use VMware PowerCLI
Minimally, to use VMware PowerCLI, all you have to do is to download and install the VMware PowerCLI application (free). You can download this from the VMware PowerCLI Community site.
Make sure that you download version 5.0 or later because, at the time of writing this article, version 5.0 is the latest and is compatible with vSphere 5.
This community site offers a lot more than just the PowerCLI software. They also house:
- the official VMware PowerCLI documentation
- a free PowerCLI Workshop presentation and scripts
- a link to the PowerCLI Blog (lots of good script examples there)
- PowerCLI sample code
- a discussion forum where you can post your PowerCLI questions
Downloading and Installing VMware vSphere PowerCLI
As I said, PowerCLI is actually just a simple Windows installation. Downloading it will require a free VMware account but will only take a few minutes. The 91MB download may take longer to download than it takes to actually install.
Once downloaded, here’s what the installation looks like:
During the installation, you may see a message that looks like this. If you do, you can click continue but you will have to resolve this later.
Once installed, you’ll have a group of programs under your VMware program group that looks like this:
To actually run PowerCLI, you would run the program described as VMware vSphere PowerCLI and here’s what you would see:
Initially, a black window with a command prompt is very intimidating for new PowerCLI admins so please don’t get scared off at this point. You don’t have to spend your whole PowerCLI life at this command prompt. There are easier ways to use PowerCLI (and I’ll show them to you later).
First, let’s fix that nasty-looking red error (the same error that popped up during installation).
Fixing PowerCLI Execution Policy Errors by Setting RemoteSigned
I’m not going to claim to be an expert on PowerCLI security nor am I even going to go into the different PowerCLI execution policy settings that are possible. Instead, I’m just going to show you how to resolve this error the quickest way possible so that you can run some PowerCLI scripts.
This annoying error seems to come up every time I go to run a PowerCLI script and I always have to Google what I need to do to resolve it. This is likely because I never fix the issue permanently and because I don’t get the opportunity to use PowerCLI as much as I would like (I don’t have a large production environment like many of you have). So, let’s not only fix this PowerCLI execution policy error but also do it “once and for all”.
As the GUI error message said when we were installing PowerCLI, to resolve this, you simply need to set the PowerCLI execution policy to remotesigned. To do it, use the same command prompt that you see in the Window in Figure 7 and type:
Here’s what it might look like:
Uh-Oh, it didn’t work. Why is that? Notice that it says because you didn’t have access to the registry, you couldn’t set this. To set this successfully, you have to open the PowerCLI prompt as Administrator (or equivalent). Let’s try it again:
Ahh, success! We shouldn’t have to deal with this error anymore.
PowerGUI, VMware Community PowerPack, & Project Onyx – Must Have Tools
Now that you have the required PowerCLI installed and the correct security policy, you need the single tool that is going to make using PowerCLI easier. Using PowerCLI from that unhelpful and boring old black command window reminds me of using MS-DOS. Trust me, that’s not how you want to use PowerCLI.
The single tool that you must have to use PowerCLI productively, luckily, is completely free and comes with a ton of pre-created scripts. That tool is PowerGUI and it includes the VMware Community PowerPack. You can download it from PowerGUI.org and the install is your quick and customary Windows install process.
While not as crucial as PowerGUI, the other tool that I highly recommend is VMware’s free Project Onyx. This tool acts as a proxy server between your vSphere Client and vCenter server. The Onyx console shows you everything you do in the vSphere Client generated as a PowerCLI script (automatically) that you can use and modify however you like.
Resources for Learning PowerCLI
Now, we are ready to start learning how to write some scripts, right? Well, first I recommend that you make sure that you check out the best PowerCLI resources available. You don’t want to try to write a script, with no preparation, get frustrated, and give up on your plan to learn PowerCLI.
Here are three resources that I highly recommend to learn PowerCLI:
- BOOK: VMware vSpherePowerCLI Reference: Automating vSphere Administration by Luc Dekens, Alan Renouf, Glenn Sizemore, Arnim van Lieshout, & Jonathan Medd
- BOOK: Managing VMware Infrastructure with Windows PowerShell by Hal Rottenberg
- VIDEO: TrainSignal’sVMware vSpherePowerCLI Video Training by Hal Rottenberg
- and, if you have significant fund to spend learning PowerCLI, VMware has their official education course, VMware vSphere: Automation with vSpherePowerCLI (still at version 4.1).
Besides these more structured & formal learning options, the authors of these books and other bloggers have excellent websites, such as:
- Luc Dekens - http://www.lucd.info
- Alan Renouf - http://www.virtu-al.net/
- Jonathan Medd - http://www.jonathanmedd.net/
- Hal Rottenberg - http://halr9000.com/
And, don’t forget the official VMware PowerCLI Documentation, now updated for version 5.0. Don’t forget to keep up on the latest PowerCLI news by following the official VMware @PowerCLI Twitter account.
One of the coolest things that VMware ever did is to create their PowerCLI poster. Even though you may not have picked up the poster at VMworld, you can print the poster for yourself to hang on your office wall. Download the new VMware PowerCLI poster here.
Finally, here at VirtualizationAdmin.com, we have a great post by Scott Lowe where he walks through some of his favorite PowerCLI scripts. Make sure you checkout Use PowerCLI to get Quick Stats Part 1 and Part 2.