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.

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

Description

DrsAutomationLevel
Displays the DRS automation level for the virtual machine. Valid values are FullyAutomated, Manual, PartiallyAutomated.
ExtensionData
Allows you to access deeper details for a virtual machine
FloppyDrives
Details about the floppy drive connected to the virtual machine
Folder
Displays the name of the folder in which the VM resides
FolderID
Displays the folder ID for the above
Guest
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.
HAIsolationResponse
Indicates whether the virtual machine should be powered off if a host determines that it is isolated from the rest of the computer resource
HardDisks
Returns a list of the hard drives you have assigned to the virtual machine.
HARestartPriority
Specify the HA restart priority of the new virtual machine. The valid values are Disabled, Low, Medium, High, and ClusterRestartPriority.
Host
Displays information about the host on which the virtual machine resides.
HostId
The ID of the host
Id
The internal ID of the virtual machine.
MemoryMB
Returns the amount of memory allocated to the virtual machine
Name
Returns the “friendly name” of the virtual machine.
NetworkAdapters
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
Notes
Displays notes that you have associated with a particular virtual machine.
NumCpu
Displays the number of virtual CPUs that have been assigned to the virtual machine.
PersistentId
A unique ID number associated with each virtual machine.
PowerState
Can be PoweredOff, PoweredOn or Suspended.
ProvisionedSpaceGB
Returns the amount of space allocated to the virtual machine.
ResourcePool
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
ResourcePoolId
The ID associated with the resource pool.
Uid
Another unique ID associated with the virtual machine.
UsbDevices
Gets a list of USB devices attached to the virtual machine
UsedSpaceGB
Amount of space actually being used by the virtual machine
VApp
VApps associated with the virtual machine
Version
Returns the virtual machine hardware version.
VMhost
Returns the same information as the Host parameter
VMHostID
Same as HostID.
VMResourceConfiguration
Displays resource allocation information associated with the virtual machine, including CPU affinity, CPU limits and more.
VMSwapfilePolicy
Displays the swap file policy for the virtual machine. This can be WithVM, Inherit or InHostDatastore.

No comments:

Post a Comment