Monday, 27 January 2014

Objective 8.1 – Execute VMware Cmdlets and Customize Scripts Using PowerCLI

Knowledge - Identify vSphere PowerCLI requirements
Windows PowerShell 2.0
A supported version of .NET Framework
     .NET Framework 2.0 with Service Pack 2
     .NET Framework 3.0 or .NET Framework 3.0 with Service Pack 1, or Service Pack 2
     .NET Framework 3.5 or .NET Framework 3.5 with Service Pack 1

Knowledge - Identify Cmdlet Concepts

A cmdlet is a lightweight command that is used in the Windows PowerShell environment. The Windows PowerShell runtime invokes these cmdlets within the context of automation scripts that are provided at the command line. The Windows PowerShell runtime also invokes them programmatically through Windows PowerShell APIs.

Windows PowerShell uses a verb-and-noun name pair to name cmdlets. For example, the Get-Command cmdlet included in Windows PowerShell is used to get all the cmdlets that are registered in the command shell. The verb identifies the action that the cmdlet performs, and the noun identifies the resource on which the cmdlet performs its action.

Knowledge - Identify Environment Variables Usage

You can use Windows environment variables as you would any variable withinscripts to access these prefix with $env:


Skills and Abilities - Install and configure vSphere PowerCLI 
Download the latest version of vSphere PowerCLI from the VMware Web site,  follow the install Wizard


Skills and Abilities - Install and configure Update Manager PowerShell Library
Download the Update Manager PowerCLI installer package from the VMware Web site,  follow the install Wizard


Skills and Abilities - Use basic and advanced Cmdlets to manage VMs and ESXi Hosts
For basic commands use PowerCLI cmd prompt,  within this form a connection to vCenter
   Connect-VIServer <hostname>

List VMs
   Get-VM



List Hosts 
  Get-VMHost




For writing more complex PowerCLI scripts use Powershell ISE,  the PowerCLI cmdlets are not added by default,  so you need to add them
   Add-PSSnapin VMware.VimAutomation.Core

Once you have the PowerCLI cmdlets available in ISE you can quickly write complex scripts, format outputs to files etc etc



Skills and Abilities - Use Web Service Access CmdletsWeb service access cmdlets return .net object data this offers you alternate properties,
$vms = Get-View -ViewType "VirtualMachine"
foreach ($vm in $vms) {
$vm.Name + "," + $vm.AlarmActionsEnabled
}


Get-VIObjectByVIView reverts .net object data to standard vSphere VIObject
$vms = Get-View -ViewType "VirtualMachine"
foreach ($vm in $vms) {
Get-VIObjectByVIView $vm
}


Skills and Abilities - Use Datastore and Inventory Providers
List all datastores and comma delimit name, capacity and freespace
$dss = Get-Datastore
"name,capacity,free"
foreach ($ds in $dss) {
$ds.Name + "," + $ds.CapacityGB + "," + $ds.FreeSpaceGB
}


Get-Inventory is useful to catch all objects rather than targeted at one Type


Skills and Abilities - Given A Sample Script, Modify The Script To Perform A Given Action
When or writing complex Powershell scripts it is useful to load into Powershell ISE and use breakpoints and step through code as you write it,  the use of breakpoints in troubleshooting a script you have written or understanding the state of variables as you step through someone elses code is really good way to begin to understand at what point things happen and once you understand the structure you can amend its functionality as you desire.

No comments:

Post a Comment