Skip to content
Tags

VMware, Scripting, Scripts, PowerShell, PowerCLI

Tips for Getting Started with PowerCLI

Feb 9, 2012

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.

spacer

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.

spacer

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.

spacer

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.

spacer

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.

spacer

This is my scripting mentor, Battle Owl.

Scripting Exercises

Try to figure out the code for the following activities. The answers are below in the next section.

  1. Power on a VM
  2. Gracefully shutdown a VM
  3. Take a snapshot of a VM
  4. Figure out how many VMs are in a Cluster
  5. 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"

Thoughts

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.

Advertisement

Share this:

  • More

    Like this:

    Like
    Be the first to like this post.

    From → Scripts, Tech Guides

    5 Comments
    1. spacer
      Luc Dekens (@LucD22) permalink

      Great PowerCLI introduction (thanks for the mention).
      I love the Exercises, there should be more like these. I believe you learn more by trying to figure out how to do something.
      Perhaps also worth mentioning, the PowerCLI Online Reference.(www.vmware.com/support/developer/PowerCLI/PowerCLI501/html/index.html)

      Reply
      • spacer
        Chris permalink

        Good call on the online reference, I didn’t think to add the link but use it all the time. Thanks for your comment!

        Reply
    2. spacer
      Victor Forde permalink

      Thanks for the post! Just the motivation I needed to break the duck on Powercli spacer but to do automation, powershell skills are needed so I found this place (www.powershellpro.com/powershell-tutorial-introduction/) for powershell tutorials in case anyone else is like me and is green when it comes to PowerCli and PowerShell.

      Reply
      • spacer
        Chris permalink

        Glad I could help motivate – I started using PowerShell “back in the day” to automate work in an AD environment.

        Also, this link looks like a good resource, thanks for sharing.

        Reply

    Trackbacks & Pingbacks

    1. Top 5 Blog Posts Of The Week 2/10/12 « vDestination: you're virtually there

    Leave a Reply Cancel reply

    Fill in your details below or click an icon to log in:

    spacer
    spacer

    You are commenting using your WordPress.com account. ( Log Out / Change )

    spacer

    You are commenting using your Twitter account. ( Log Out / Change )

    spacer

    You are commenting using your Facebook account. ( Log Out / Change )

    Cancel

    Connecting to %s

    « Tackling the Craziness: Microsoft Licensing Limitations on vMotion
    ADSI Edit to the Rescue: Modifying VM Folder Settings in VMware View »
    Related searches:
    powercli scripting vmware twitter network
    gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.