Wednesday, 25 July 2012

VMware Automation Tools: PowerCLI - Part1


PowerCLI

Before starting with PowerCLI I would like to introduce Windows PowerShell. The reason is that PowerCLI is built on top of PowerShell.

PowerShell 3.0 can be downloaded from the following link:

http://www.microsoft.com/en-us/download/details.aspx?id=29939
PowerShell (PS)

The first building block of PS is cmdlet (command-let). There is a default set of cmdlets which is available with the installation of PS 2.0 (core library of PS 2.0). Other technologies are having their own set of cmdlets (their own libraries and PS modules). To have them integrated with PS, they should be added as snap-ins (e.g. VMware PowerCLI which we will cover later).

Whether cmdlet from core library is used or from third party module, the same cmdlet structure is always followed.


Cmdlets always consist of a verb-noun compound phrase separated by a hyphen. In the previous example, Get-ChildItem, the Get (verb) part of the command directs the action; in this case, to retrieve data. The ChildItem (noun) part of the command provides the focus for the verb; in the example, to obtain a directory listing.

Note: Cmdlets are case in-sensitive

Most PowerShell cmdlets support one or more parameters and arguments (parameters with arguments are called named parameters). A parameter represents additional data that is passed into a cmdlet to customize its operation. An argument is nothing more than a value that is passed to a parameter.

Note: Some parameters aren't having '-'. Also, not all parameters require arguments which are called positional parameters.

Important Tips:

  •  get-help <xxx>, where xxx is any keyword within cmdlet syntax
This will list all cmdlets which includes xxx in their syntax

  • get-command <aaa>, where aaa is cmdlet syntax
This will show all the parameters and arguments which can be assigned to the cmdlet

  • get-help * will list all commands

Example

PS C:\Users\mohammed.albaqari> get-help restart

Name                              Category  Synopsis
----                              --------  --------
Restart-Service                   Cmdlet    Stops and then starts one or more services.
Restart-Computer                  Cmdlet    Restarts ("reboots") the operating system on local and remote computers.


PS C:\Users\mohammed.albaqari> get-command restart-service

CommandType     Name                                                Definition
-----------     ----                                                ----------
Cmdlet          Restart-Service                                     Restart-Service [-Name] <String[]> [-Force] [-Pa...

Here are some common verbs used in cmdlets:
  • Get
  • Set
  • Add
  • Remove
  • Clear
  • Enable
  • Disable
  • Start
  • Restart
  • Resume
  • Stop

The second important block of PS is pipeline. The pipeline concept allow passing the result of one cmdlet as an argument to the second cmdlet. Therefore, the pipeline enables us to create compound cmdlet sequences that perform multiple tasks in a single operation. Take a look at the following example:

PS provide alias shortcuts to cmdlets. Those can be system-defined or user-defined. You can verify the available aliases using the cmdlet Get-Alias. Also, you can create user-defined alias using Set-Alias cmdlet.

PS Script

It is a text file with the following characteristics:
  1. It has the .ps1 file extension
  2. It contains one or more lines of PowerShell source code.

PS Scripts aren't compiled at run-time but rather interpreted, line-by-line, by the PowerShell interpreter. Therefore, you will need PS on the target system which is running the script.

PS scripts are written using PS Integrated Scripting Environment (ISE). PS ISE has three viewing panes used for the following purposes:
  • Script Pane: Enables you to edit your PowerShell scripts
  • Output Pane: Displays the output from your scripts
  • Command Pane: Enables you to issue PowerShell statements interactively

To run a PS script from outside PS ISE, we need to set the appropriate script execution policy. There are four modes for script execution policy:

  • Restricted: Windows PowerShell can be used only in interactive mode from a PowerShell prompt or from an ISE. Scripts aren't allowed to run freely on the host.
  • AllSigned: Only scripts that are digitally signed by a trusted publisher are allowed to run
  • RemoteSigned: Locally created scripts can run fine; downloaded scripts must be digitally signed by a trusted publisher in order to run
  • Unrestricted: All PowerShell scripts can run without restriction (not recommended for most environments)

You can use the command Set-ExecutionPolicy to change the current policy and Get-ExecutionPolicy to view the current policy.

To run the scripts, different methods can be used:

  1. From windows command prompt navigate to the script and type its name (e.g. C:\Scripts\<script.ps1> )
  2. From PS ISE use Invoke-Expression command (e.g. PS> Invoke-Expression “C:\Scripts\<script>.ps1”)

No comments:

Post a Comment