Getting started with PowerCLI

I’m setting up a new computer and thought to myself “hey self, this might be a good time to write your first PowerCLI post”

The first post was going to be about how to customize your profile, but why recreate the wheel. Check out awesome post on how to customize your PowerShell profile.

With that being said if you’re actually reading this, chances are you’re really bored or you’re newish to PowerCLI/PowerShell, so we are going to start back at basics. If it’s the later the first thing you’re going to want to do is update PowerShell to the latest version. PowerShell 2 was interesting, and PowerShell 3 was a huge improvement but at this point you really shouldn’t be running anything less than PowerShell 4.0.  To see what version you’re running, you could simply enter $PSVersionTable and hit enter. However what you’re really interested in is the major version that can be accessed by entering the following and hitting enter.

$PSVersionTable.PSVersion.Major

The output from this variable is the primary version of PowerShell that you’re running

sidenote: If you’re paying close attention, you probably noticed that PowerShell has tab complete functionality that will be one of your best friends as you move along with your PowerShell adventures.

2016_11_29_13_14_39_administrator_windows_powershell_ise

We just established that you’re paying attention, so you’ve also noticed that the above screenshot is not just the PowerShell command interpreter. What you’re looking at it is PowerShell ISE (Integrated Scripting Environment). It’s a scripting pane (the top part where you write the script), along with the command interpreter and a command window, but if you know how to use the help commandlet (we’ll get there) the command window is largely useless IMHO.

Now since you are an astute reader you’d probably first ask yourself “how do I get this ISE thing?”. Why it’s not installed by default, I’ll never know, but if you want to use it (you do) then you’ll need to add from the Windows feature menu.

Secondly you’re probably saying, isn’t this supposed to be about PowerCLI? We’re getting there, be patient! These are a couple of the items that have helped me be successful when I was first starting to write PowerShell/PowerCLI scripts.

OK, PowerCLI. You’ll need to logon to your my.vmware.com account to get the latest release, 6.5 as of this writing. You should check out the VMware PowerCLI blog as well. Lots of great information about the releases, and you can pick up some good tips along the way. IF you already have scripts written for earlier versions of PowerCLI, be cautious about the version you install. Why? Let me tell you.

The kind folks at VMware have been working to convert from snap-ins to modules and as of a few days ago that conversion appears to be complete. This is great for you because the scripts that you’re going to write, just became a whole lot more portable and therefore useful. BUT if you’re using scripts that are already built,  that leverage snap-ins AND you upgrade to a PowerCLI version that has converted that functionality to modules… uh oh…

Now that you’ve finally installed PowerCLI you fire it up and …. wait, where’s my ISE window? VMware does not give you a PowerCLI_ISE window, so you fire up good old PowerShell_ISE and … what? you don’t have the PowerCLI commandlets available to you??? Who’s idea of a cruel prank is this? So you enter the command

Get-Module -ListAvailable | Where-Object {$_.name.Contains(“VMware”)}

and when you hit enter, phew, you’ve finally got some stuff to work with.

posercli_modules

To load any of the modules, you simply enter the command import-module . For example to leverage many of the common vCenter tasks, you’d load the core module via the command:

import-module VMware.VimAutomation.Core

I don’t know about you, but I don’t like repeating myself. The way we avoid having to run the commands to load modules every time we want to write a script is via our PowerShell profile. (see what I did there, connected it all together I did)**   I’m going to presume that you at least skimmed the article referenced at the top of this post, so you already have a PowerShell profile that can be accessed by running notepad $profile in a PowerShell window. Simply enter the commands that you want run when you launch PowerShell hit ctrl-s and BLAMMO you get your PowerCLI modules loaded automagically every time that you launch PowerShell!

If you find this useful, please let me know. I’m planning on writing a series of PowerShell/PowerCLI posts for beginners, so your feedback is really appreciated.

Have a great one!

**read in your best yoda voice

One thought on “Getting started with PowerCLI

Leave a comment