Getting Started with PowerCLI

If you haven’t already done so, please go ahead & install PowerCLI from the VMware website. It is simple and straight forward to install PowerCLI. I doubt you should get stuck during installation, but just in case you do, refer HERE or Google around and you should find many articles talking about the installation.

Once you have installed PowerCLI, before we begin, we need to configure PowerShell so that we can execute PowerCLI cmdlets without any difficulty. The default execution policy of PowerShell is “Restricted” which would disallow execution of all PowerShell script files. Now this policy would not allow us to run any PowerCLI cmdlets. So we have to set it to a less obstructive policy which is “RemoteSigned”. This would allow us to run scripts signed by a trusted entity. Here is how we can set the execution policy:

  1. Right Click on Start–>Programs–>Accessories–>Windows PowerShell–>Windows PowerShell
  2. Select “Run as administrator” to start an administrative PowerShell session
  3. On the PowerShell command prompt, type:
Set-ExecutionPolicy RemoteSigned

You can verify if the execution policy was correctly set, by typing the following command:

Get-ExecutionPolicy

You can read more about the execution policy by using the Get-Help cmdlet:

Get-Help about_Execution_Policies

Now that’s was more than a page of help. To paginate the output, you can use more function:

Get-Help about_Execution_Policies | more

By now, you should have realized that tab completion works in a PowerShell environment, type initial few characters of a word and press TAB and it will complete the word or cmdlet for you.

Another way to get help would be to use the help command which is actually an alias for the Get-Help.

Help about_Execution_Policies | more

I am assuming you have already installed PowerCLI on your admin desktop. There are a couple of ways to start your PowerCLI session:

  1. Method one, double click on the ‘VMware vSphere PowerCLI’ icon that was created on your desktop and you should be started.
  2. Alternative method would be, at the PowerShell command prompt, type:
Add-PSSnapin VMware.VIMAutomation.Core

This would load the PowerCLI Snap-in, and in turn allow you to access the vSphere PowerCLI cmdlets.

You can verify if your Snap-in is loaded by using the Get-PSSnapin cmdlet.

Get-PSSnapin VMware.VIMAutomation.Core

Running Get-PSSnapin cmdlet without arguments would let us know what PowerShell Snap-ins are currently loaded.

Now that we know how to start PowerCLI, let’s look at a cmdlets that we will use often:

Get-Help: Get-Help is a command that prints the help for a command argument. By default, if used without any arguments it will print the help for Get-Help.

You can get detailed help on cmdlets by using the following named parameter:

Get-Help Get-Command -full

If you need example of a command line then try this:

Get-Help Get-Alias -Examples

Let’s quickly discuss what is the format for the PowerShell (& also PowerCLI) command line?

A PowerShell command line is made of:

<cmdletName> [cmdletParameters] [cmdletArguments]

The string within angle brackets indicates a required string which is the cmdlet name and the string within the square brackets indicate an optional string which could be named parameters or cmdlet arguments.

A cmdlet is a complex word, made of two parts, the Verb and Noun separated by a hyphen. Where Verb defines the actions, whereas the noun defines on what entity you want to perform the said action. A cmdlet is of the form:

Verb-Noun Arg1 –namedParameter Param1

In the above mentioned command line, the first part is the cmdletName, followed by the second part, the argument to the cmdlet. This is followed with additional modifiers to the cmdlet known as namedParameters. A cmdlet executes in the context of the Argument and the named parameters modify the default behavior of the cmdlet. The named parameters for some commands could be required parameters.

Remember, there could be one or many named parameters and arguments. The named parameters could also be of Boolean type. Where the named parameter acts like a true/false (on/off) switch.

Everything in PowerShell is an Object. A cmdlet (pronounced as command-let) is a command object that you execute in a PowerShell environment. Cmdlets allow you to do some work or execute a task.

Objects have properties and methods. Properties are made of Key/Value pairs. You use the methods to query or modify the properties.

Let’s assume that we want to identify all those cmdlets which are being provided by Say vSphere PowerCLI. Here’s how you can do that:

Rephrasing the question can we ask say, list all cmdlets that are provided by the VMware.VIMAutomation.Core Snapin.

$SnapinName = ‘VMware.VIMAutomation.Core’;
Get-Command | Where-Object { $_.PSSnapin.Name –eq $SnapinName }

Look at the above code:

Line1: Defines a variable and assigns a value, here we are storing the name (a string) of the PowerShell Snapin in variable named $SnapinName. The last semicolon indicates a end of line in PowerShell. The semicolon is optional.

Line2:  Execute Get-Command and pass the output using the pipe. Get-Command prints a collection of cmdlets, functions & aliases (objects). Receive the output from the pipe (|) and filter it using the Where-Object. Where-Object cmdlet processing stops on seeing the closing curly brace. The code between the opening curly brace and the closing curly brace is known as the filter block.

Since we are discussing about the Where-Object, lets take some time to know more about it. Where-Object allows filtering of output based on criteria. Here we print only those cmdlets whose provider Snapin name matches the contents of the variable $SnapinName. Storing the Snapin name within a variable and using it for comparison on Line4 makes the code easy to understand and maintain.

The $_ (dollar-underscore) is a special variable in PowerShell and is a reference in context of the current object.

Consider the following sentence:

This is an Apple. It is red in color.

In the above statement, ‘It’ is a reference in context of the Apple.

Refer to Line2 in the above code block. Here Where-Object received a collection of objects from the pipe and it feeds one object at a time to the filter block. $_ refers to each object from the collection at a time. In this example $_ is referring to each object being sent from the Get-Command cmdlet. So we could have written the code to make it more readable as:

$SnapinName = ‘VMware.VIMAutomation.Core’
Get-Command | Where-Object {
$myCmdlet = $_
$myCmdlet.PSSnapin.Name –eq $sName
}

Because this a very small piece of code we are not compromising the readability by using the shortcut of not defining the variable on Line2. However In bigger code blocks, it’s a good idea to define a variable instead of using $_ directly.

Now since I wanted to introduce you to some PowerShell code we looked at the above mentioned example. There is another way to find commands that belong to a specific PSSnapin, e.g.

$SnapinName = ‘VMware.VIMAutomation.Core’
Get-Command -Module $SnapinName

Let us say if we want to count the number of cmdlets provided by the said PSSnapin, here’s how we can do that:

$SnapinName = ‘VMware.VIMAutomation.Core’
Get-Command -Module $sName |  Measure-Object -Line

Measure-Object is a cmdlet allows us to count the output. We saw the example for counting lines, but also count for words and characters.

Another useful trick of the Get-Help cmdlet is for finding cmdlets with a specific string in them, say we want to find all the cmdlets which contain the string ‘vmhost’, here how we can find such cmdlets:

help *vmhost*

In the above command we are saying, list all the commands which starts with any string, ends with any string and contains the string ‘vmhost’. Also note that instead of using the cmdlet Get-Help, we are using its alias.

Please note that the Where-Object & Measure-Object are native cmdlet of PowerShell and not provided by PowerCLI. There are other native cmdlets PowerShell cmdlets that we will be using in this guide. Now this is not a PowerShell guide, however we will look at some native cmdlets and discuss them along the way. In addition, I would encourage you to take some time and read through the PowerShell documentation. For your reference I have listed out a few links on the resource list page.

Exercises:

  1. Read the help along with the cmdlet examples for cmdlet Get-Command.
    Hint: to get cmdlet examples use the named parameter -examples along with the Get-Help cmdlet.
  2. Read the help for Measure-Object and count the number words in the help text for Get-Command.
  3. List all those cmdlets which contain the VMHostService string.
  4. List cmdlets that start with the word Stop.
  5. Find the aliases for the cmdlets Get-Command, Measure-Object and Where-Object.
  6. Execute the above exercises using the aliases.

I hope, you have been reading along, keep watching this space for additional articles.

Advertisements

Leave a Reply

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

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s