Saturday, 28 July 2012

VMware Automation Tools: PowerCLI - Part2

PowerCLI is built on top of MS PowerShell as snap-in. The name of the snap-in is Vmware.VimAutomation.core. It includes around 260+ cmdlets to manage vSphere environment. On the startup of PowerCLI, PS will add the snap-in using the following command

AddPsSnapin Vmware.VimAutomation.core

Installing PowerCLI

  1. Make sure that you have PS on your system which is a core component of Win7. For older systems, it can be downloaded offline from MS website.
  1. Download PowerCLI from VMware website
  1. Run the installer and complete the installation of PowerCLI
  2. Change the execution policy of PS scripts in your system if not already done. For PowerCLI to run properly, the policy should be changed to RemoteSigned. This will popup as well during PowerCLI installation to change the policy to RemoteSigned.

Note: In case the policy isn't changed, PowerCLI will fail to load. The reason is that PowerCLI will run some startup scripts to add snap-in to PS in order to get cmdlets which will be blocked based on PS policy.
Working with Objects

Each component in vSphere environment is considered as object such as cluster, VM, snapshot, template, host, etc. Using PowerCLI you can perform multiple actions on the objects such as add, remove, edit, etc.

The first step when working with vSphere Environment using PowerCLI is to connect to it. This is done by connecting to vCenter server or ESXi host directly.

Connect-VIServer –Server <vCenter Server hostname> –Username <Username> –Password <password>

Working with Hosts as Objects

To get info about the hosts in the environment, using the command Get-VmHost.
The output of this cmdlet is showing general info in table format. However, much more details can be listed per host in case the format of the output is changed. You can reformat it using the cmdlet Format-List which is having alias as fl.
Working with VMs as Objects

To get info about VMs hosted on ESXi hosts, you can use the command Get-VM.
Working with Guest as Object

PowerCLI can go beyond the VM to manage the guest OS as well. For example, you can use Get-VmGuest cmdlet to list the detail about OS. Also, Get-VmGuestRoute and Get-VmGuestNetworkInterface commands can be used.
Note: Those commands require VMtools to be installed and running on the guest OS.

Working with other Objects

We will look here at hard disks and vNICs used by VMs. To look at the VMDKs from datastore and virtual machine point of view, you can use the command Get-HardDisk <virtual machine name> and Get-HardDisk -Datastore <datastore name>.
To list all NICs associated with VMs host on one host using the command Get-VmHost <host address> | Get-Vm <vm name> | Get-NetworkAdapter
To list all the commands added by PowerCLI snap-in to PS, use the command Get-VICommand. You can be also the command Get-Command –Module VMware* -Verb Get to list all get cmdlets. Again cmdlet Get-Command –Noun VM can be used to list all cmdlets having VM in their noun portion.

Dig Deeper

I would like to examine one of the most powerful cmdlets which is Get-Member. This command can't be used alone since it needs to have its input as an object from proceeding cmdlet in the pipeline string. It will list all properties and methods which can be used with the input object.

In those examples we will list all properties and methods which can be used with virtual machine object as well as hard disk object.

But what is the benefit of knowing the properties and methods which can be used with input object? How to use them?

To use them you need to assign your object to a variable string as follow:

$<variable name> = <cmdlet + argument>

Then you need to apply the property to your variable as follow:

$<variable name>.<property>

Let's take some examples to understand more.
Note: There are still some bugs in PowerCLI 5.0 where some cmdlets doesn't show results.

In the next table I am listing all properties which can be used with Virtual Machine object.

A list of the CD/DVD devices connected to the virtual machine
A list of any custom fields that have been created for the virtual machine


Displays the DRS automation level for the virtual machine. Valid values are FullyAutomated, Manual, PartiallyAutomated.
Allows you to access deeper details for a virtual machine
Details about the floppy drive connected to the virtual machine
Displays the name of the folder in which the VM resides
Displays the folder ID for the above
When used by itself (i.e. $v.guest | fl), returns a full list of guest virtual machine parameters such as the name of the operating system, the guest machine’s IP address, list of assigned disks, dimensions of the guest OS display and other information.
Indicates whether the virtual machine should be powered off if a host determines that it is isolated from the rest of the computer resource
Returns a list of the hard drives you have assigned to the virtual machine.
Specify the HA restart priority of the new virtual machine. The valid values are Disabled, Low, Medium, High, and ClusterRestartPriority.
Displays information about the host on which the virtual machine resides.
The ID of the host
The internal ID of the virtual machine.
Returns the amount of memory allocated to the virtual machine
Returns the “friendly name” of the virtual machine.
Displays the network adapters that are currently installed in the virtual machine along with the network adapter name, type, virtual network name, MAC address and whether or not Wake On LAN is enabled
Displays notes that you have associated with a particular virtual machine.
Displays the number of virtual CPUs that have been assigned to the virtual machine.
A unique ID number associated with each virtual machine.
Can be PoweredOff, PoweredOn or Suspended.
Returns the amount of space allocated to the virtual machine.
Displays the name of the resource pool in which the virtual machine exists. You can get a lot of additional information about the resource pool
The ID associated with the resource pool.
Another unique ID associated with the virtual machine.
Gets a list of USB devices attached to the virtual machine
Amount of space actually being used by the virtual machine
VApps associated with the virtual machine
Returns the virtual machine hardware version.
Returns the same information as the Host parameter
Same as HostID.
Displays resource allocation information associated with the virtual machine, including CPU affinity, CPU limits and more.
Displays the swap file policy for the virtual machine. This can be WithVM, Inherit or InHostDatastore.

No comments:

Post a Comment