Monday, February 6, 2012

vSphere PowerCLI Blog: PowerCLI 5.0.1 vCloud Director – Stopping and starting vApps

vSphere PowerCLI Blog: PowerCLI 5.0.1 vCloud Director – Stopping and starting vApps:

In a previous video we showed you how to get started with PowerCLI 5.0.1 and specifically the vCloud Director (vCD) cmdlets, we shows you how to gain a list of cmdlets which were added to PowerCLI 5.0.1 for managing vCD and how to use some of these cmdlets to gather data from your vCD infrastructure.


I received some comments from people saying this was great information and they could use this to write some scripts which would allow them to pull data from the vCD infrastructure, but how could they interact with vCD ? At the moment the web interface gives them access to not only view the cloud data but also interact with it, stop/start vApp’s and VM’s, create new Organization’s, ProvidervDC’s, OrgvDC’s and much much more.


You may not know but this functionality is actually available in the PowerCLI 5.0.1 cmdlets. When listing the cmdlets you will see that most start with “Get-“ which means they are mainly designed to retrieve the data. People who have looked a little further or perhaps have slightly more PowerCLI knowledge will have noticed the cmdlet Get-CIView.


The Get-CIView cmdlet allows us to reach into the full SDK for vCloud Director and pull back the full .Net object, this basically means that we get access to perform anything in vCloud Director that you would normally do via the web interface.


Another way to retrieve the .Net View representation of a PowerCLI object is to refer to its .ExtensionData property.


The .ExtensionData property is an “on-demand” property which gives us the same results as calling the Get-CIView cmdlet.


To see a simple example of how we might use this .Extensiondata property to interact with vCD and Start/Stop a vApp take a look at the below video example:



From this simple example we can then create PowerShell advanced functions to make this easier for us in the future as can be seen below, this will give us 2 functions which will allow us to easily stop and start vApps in the cloud using PowerCLI.


function Stop-CIVApp {
<#
.SYNOPSIS
Powers Off a vApp.


.DESCRIPTION
Powers Off a vApp and all VMs within it.


.PARAMETER CIVApp
The Name or object or collection of objects of vApps which to shutdown.

.PARAMETER Force
Attempt to power off the VMs. Failures in undeploying the VM or associated networks are ignored.
All references to the vApp and its VMs are removed from the database


.EXAMPLE
PS C:\> Get-CIVApp | Stop-CIVApp


.EXAMPLE
PS C:\> Get-CIVApp "MyvApp" | Stop-CIVApp

.EXAMPLE
PS C:\> Stop-CIVApp -CIVApp "MyvApp"

.EXAMPLE
PS C:\> Stop-CIVApp -CIVApp "MyvAPP" -Force
#>
[CmdletBinding(
SupportsShouldProcess=$true,
ConfirmImpact="High"
)]
param(
[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
$CIVApp,
[Switch]$Force
)
Process {
if ($pscmdlet.ShouldProcess($CIVAPP)) {
If ($Force){
$CIVApp | Foreach {
$_.ExtensionData.Undeploy("force")
}
} Else {
$CIVApp | Foreach {
$_.ExtensionData.Undeploy("powerOff")
}
}
}
}
}



function Start-CIVApp {
<#
.SYNOPSIS
Powers On a vApp.


.DESCRIPTION
Powers On a vApp and all VMs within it.


.PARAMETER CIVApp
The Name or object or collection of objects of vApps which to start.


.EXAMPLE
PS C:\> Get-CIVApp | Start-CIVApp


.EXAMPLE
PS C:\> Get-CIVApp "MyvApp" | Start-CIVApp

.EXAMPLE
PS C:\> Start-CIVApp -CIVApp
#>
[cmdletbinding()]
param(
[parameter(ValueFromPipeline=$true)]
$CIVApp
)
Process {
$CIVApp | Foreach {
$_.ExtensionData.PowerOn()
}
}
}

No comments:

Post a Comment