Information Technology Reference
In-Depth Information
There is so much more that you can do with PowerShell and PowerCLI; these simple exam-
ples barely scratch the surface. In the next section, we'll look at some of the advanced capabili-
ties available to automate vSphere with PowerCLI.
PowerCLI Advanced Capabilities
You should now have a fair understanding of the many possibilities for automating a vSphere
environment with PowerCLI. We'd like to spend a little time demonstrating some advanced
functionality that is available to you to directly leverage the vSphere vCenter API.
PowerCLI users are not limited to just the cmdlets included in PowerCLI 5.5. VMware
extends the capabilities of PowerCLI by allowing users to access various methods and informa-
tion within the vSphere API. The Get-View cmdlet gives PowerCLI users the ability to call these
methods as part of their PowerCLI scripts or directly from the PowerShell console.
In Listing 14.2, we share a situation where we recognized functionality in the vCenter server
that was not natively available in the PowerCLI cmdlets. Specii cally, when attempting to vMo-
tion a VM, we recognized that vCenter would perform checks to verify that the prerequisites for
vMotion to that host were met. If something was found to be incorrect, vCenter would notify the
administrator that vMotion could not be performed and generally identify the root cause. What
we wanted to accomplish was to check every VM in a cluster prior to a scheduled maintenance
window, allowing us to identify any issues prior to the scheduled maintenance and address
them to minimize delays or the need to roll back.
Listing 14.2: A PowerCLI function to test vMotion capabilities of VMs
Function Test-vMotion{
param(
[CmdletBinding()]
[Parameter(ValueFromPipeline=$true,Position=0,Mandatory=$false,`
HelpMessage=”Enter the Cluster to be checked”)]
[PSObject[]]$Cluster,
[Parameter(ValueFromPipeline=$false,Position=1,Mandatory=$false,`
HelpMessage=”Enter the VM you wish to migrate”)]
[PSObject[]]$VM,
[Parameter(ValueFromPipeline=$false,Position=2,Mandatory=$false,`
HelpMessage=”Enter the Destination Host”)]
[PSObject[]]$VMHost,
[Parameter(ValueFromPipeline=$false,Position=3,Mandatory=$false,`
HelpMessage=”Set to false to Turn off console writing for use in Scheduled Tasks”)]
[Boolean]$Console=$true
)
$report = @()
#Sets information based on type of work being done.
#Whole cluster or single VM
If($Cluster -ne $null){
If($VM -ne $null){
If($Console = $true){
Write-Host “VM value $VM cannot be used`
when using -Cluster paramenter. Value is being set to null”
}
 
Search WWH ::




Custom Search