A number of folks I speak with are aware of PowerCLI (and subsequently, PowerShell) and that it has some sort of magic mojo to do whatever the imagination can come up with. Which is true (mostly). The fine folks at VMware and the rockstars of scripting (mentioned later in this post) have pretty much taken PowerCLI to the point where the next version will become sentient and vSkyNet will be birthed.
However, that does you no good if you’re on the other side of the glass of PowerCLI utopia. Sometimes the hardest part to getting started is figuring out the first step. So, this post will go over some suggestions on how to set up your workstation to form a PowerCLI zen garden and some great resources (both online and in dead tree form) to help walk the path to enlightenment. Once you have a basic level understanding of how this scripting language works, mental sanity will be restored and a guy from Western Union will show up with a seventy year old letter stating that Doc is still alive and living in the wild west of 1885.
You don’t have to wear this to learn PowerCLI, but it helps
Go Grab PowerCLI!
Make sure you have PowerCLI installed before we go any further. So, go download and then install it. Preferably in that order.
We’re not actually going to use the “stock” version of PowerCLI in this tutorial so don’t worry about launching any applications after you’ve finished installing.
There’s just no sizzle with the stock editor.
PowerGUI – 1.21 Giga Watts of Power
This section could reasonably be entitled “use a scripting editor” but, quite frankly, I think PowerGUI is a great tool and highly advocate its use. The free version is amazingly well written with lots of community support and rich with features.
The main thing I like about PowerGUI is that it’s extremely stable. I’ve used a number of different GUI editors and have been disappointed with bugs or “flashy graphics” that offer no value. For the money (free) this is just one of the best. Even with a 60 second auto save built in and my habit of saving constantly, there’s nothing worse than having your editor puke in the middle of a flash of code to really put a rain cloud over your head.
Also, like any good editor, this one offers a nice set of window panes for variables / console / editing, auto-complete for cmdlets, and coloration of the code. There’s a whole slew of other features, such as PowerPacks and tabs, but these are the meat and potatoes.
Meat, Potatoes … and scripting.
Research Material and Rockstars
There are a number of great resources available online to help you learn PowerCLI. My main source when I was learning consisted of the three rockstars of scripting: William Lam (Virtually Ghetto), Luc Dekens (LucD Notes), and Alan Renouf (Virtu-Al.net). Very humble guys who donate a lot of their time to evangelize for the community and help people solve scripting road blocks on their websites, VMTN forums, via twitter, and who knows else where. I highly recommend following them on twitter and reading their blogs as a great way to start.
Additionally, Alan and Luc wrote a great book on the subject called “VMware vSphere PowerCLI Reference: Automating vSphere Administration” (Amazon) that I own and use frequently. I call it the PowerCLI bible. Go get it, it’s like 20-something bucks. Easily worth it.
A treasure of scripting knowledge is behind this cover
Learning to Script
Aside from formal course work, the best advice I can give is to snag other scripts and reverse engineer them. The book shown above has a lot of example scripts and explains how they work. Use this is a blueprint to begin making your own scripts. Another tip is to start simple and try to emulate many of the features that the vSphere client performs for you – you should already be familiar with how these work, so it’s just a matter of learning the language of PowerCLI. Of course, it helps when you’re not doing this in production, so make sure to pick a lab environment if you’re not 100% confident in your code (which should be the case if you’re just starting off!). The best way to firewall yourself from doing any harm is to script directly against a host that has only lab VMs on it, or a lab vCenter (if you have that luxury).
Additionally, check the PowerCLI VMTN forums for clues. Many of the scripts that you want have already been requested and fullfilled by a number of folks that want to help. I don’t advise simply plopping down a post that says “do it for me” – I typically ignore these. However, if you post your script and ask specific questions, such as “why am I getting this error” or “what am I missing” you should get plenty of help.
If you can find a mentor in your office or on twitter, that’s a great start as well.
This is my scripting mentor, Battle Owl.
Try to figure out the code for the following activities. The answers are below in the next section.
- Power on a VM
- Gracefully shutdown a VM
- Take a snapshot of a VM
- Figure out how many VMs are in a Cluster
- Rename a VM
Scripting Exercise Answers
Power on a VM
Start-VM -VM "name"
Gracefully shutdown a VM
Shutdown-VMGuest -VM "name"
Take a snapshot of a VM
New-Snapshot -Name "WahlNetwork Snapshot" -VM "name"
Figure out how many VMs are in a Cluster
(Get-VM -Location (Get-Cluster "cluster name")).count
Rename a VM
Set-VM -VM "name" -Name "Wahl Network VM"
As you can see from the answers, much of the code is just plain English. Nothing to be afraid of! I hope this begins your journey towards using (and loving) PowerCLI.