While writing this book, there is no Remove-CIVM
cmdlet available in PowerCLI. To remove a vCloud Director or a vCloud Air virtual machine, or do anything else in vCloud Director or vCloud Air for which there is no PowerCLI cmdlet, you will have to use the vCloud API. PowerCLI provides us the Get-CIView
cmdlet that gives access to the cloud view objects of the vCloud API. You can also use the ExtensionData
property of the vCloud objects, to access the vCloud API, just like you can do with vSphere objects to access the vSphere API. The syntax of the Get-CIView
cmdlet is as follows. The first parameter set is to retrieve cloud view objects from a cloud object:
Get-CIView [-CIObject] <CIObject[]> [-Server <CIServer[]>]
[<CommonParameters>]
The -CIObject
parameter is required.
The second parameter set is to retrieve cloud view objects by ID:
Get-CIView -Id <String[]> [-ViewLevel <CIViewLevel>]
[-Server <CIServer[]>] [<CommonParameters>]
The -Id
parameter is required.
The third parameter is to retrieve cloud view objects from CISearchResult
objects returned by the Search-Cloud
cmdlet:
Get-CIView [-ViewLevel <CIViewLevel>] -SearchResult
<CISearchResult[]> [<CommonParameters>]
The -SearchResult
parameter is required.
In the following sections Removing vCloud virtual machines, Removing vCloud virtual appliances, Creating snapshots , Retrieving snapshots, Reverting to snapshots, and Removing snapshots, we will give some examples of using the vCloud API.
In this section, we will use the vCloud API to remove the Server001
virtual machine. First, we will retrieve the vCloud virtual machine Server001
and save it in the variable $CIVM
:
PowerCLI C:> $CIVM = Get-CIVM -Name Server001
We will now use the Get-CIView
cmdlet to retrieve the cloud view object for Server001
and save it in the variable $CIVMView
:
PowerCLI C:> $CIVMView = Get-CIView -CIObject $CIVM
If you pipe the $CIVMView
variable to the Get-Member
cmdlet, you will get a list of all of the methods available. Because there are too many methods to show them in this book, we will filter for methods with a name starting with Delete
:
PowerCLI C:> $CIVMView | Get-Member | >> Where-Object Name -like 'Delete*' TypeName: VMware.VimAutomation.Cloud.Views.Vm Name MemberType Definition ---- ---------- ---------- Delete Method void Delete() Delete_Task Method VMware.VimAutomation.Cloud.Views.Task Delete_Task()
The Delete()
method will remove a cloud virtual machine on a synchronous way. It will return after finishing the delete. The Delete_Task()
method will remove a cloud virtual machine in an asynchronous way. It creates a delete task and will return without waiting for the task to complete. In the following command, we will use the Delete()
method to remove Server001
:
PowerCLI C:> $CIVMView.Delete()
The preceding command does not return any output.
Finally, we will retrieve all of the virtual machines to show that Server001
has been removed:
PowerCLI C:> Get-CIVM Name Status GuestOSFullName CpuCount MemoryGB ---- ------ --------------- -------- -------- CentOS64-64BIT PoweredOff CentOS 4/5/6/7 (64-bit) 1 1.000 CentOs-Mini PoweredOn CentOS 4/5/6/7 (64-bit) 1 1.000
As you can see in the preceding output, Server001
has been deleted.
The deletion of Server001
could have been done in a PowerCLI one-liner with the following command:
PowerCLI C:> (Get-CIVM -Name Server001 | Get-CIView).Delete()
An alternative to the preceding command would use the ExtensionData
property, as shown in the following command:
PowerCLI C:> (Get-CIVM -Name Server001).ExtensionData.Delete()
After removing Server001
in the preceding section, Removing vCloud virtual machines, we will remove the empty virtual appliance vApp001
in this section. The way we do this is similar to deleting a vCloud virtual machine. First, we will retrieve vApp001
and save it in the variable $CIVApp
:
PowerCLI C:> $CIVApp = Get-CIVApp -Name vApp001
Next, we will retrieve the CIView of vApp001
and save it in the variable $CIVAppView
:
PowerCLI C:> $CIVAppView = $CIVApp | Get-CIView
The Get-Member
cmdlet will be used to find methods that start with Delete
.
PowerCLI C:> $CIVAppView | Get-Member | >> Where-Object Name -like 'Delete*' TypeName: VMware.VimAutomation.Cloud.Views.VApp Name MemberType Definition ---- ---------- ---------- Delete Method void Delete() Delete_Task Method VMware.VimAutomation.Cloud.Views.Task Dele...
The preceding output shows us two method names starting with Delete
. The Delete()
method works in a synchronous way. The Delete_Task()
method creates a vSphere task to delete the vApp and returns right after creating the task. It does not wait until the deletion completes. In the following command, we will use the Delete()
method to delete vApp001
:
PowerCLI C:> $CIVAppView.Delete()
The preceding command does not return any output.
We will now use the Get-CIVApp
command to retrieve all of the vApps:
PowerCLI C:> Get-CIVApp Name Enabled InMaintenanceMode Owner ---- ------- ----------------- ----- student501-Cent-OS-Mini True False system vApp002 True False student501@vcaho...
As you can see in the preceding output, vApp001
has been deleted.
Just like there is no native PowerCLI cmdlet to remove cloud virtual machines, there are also no cmdlets to create and remove cloud virtual machine snapshots. So, we have to use the vCloud API also to manage snapshots. In this section, we will create a snapshot for cloud virtual machine CentOS64-64BIT
. First, we will retrieve the vCloud virtual machine CentOS64-64BIT
and save it in the variable $CIVM
:
PowerCLI C:> $CIVM = Get-CIVM -Name CentOS64-64BIT
We will now use the Get-CIView
cmdlet to retrieve the cloud view object for CentOS64-64BIT
and save it in the variable $CIVMView
:
PowerCLI C:> $CIVMView = Get-CIView -CIObject $CIVM
The Get-Member
cmdlet will be used to retrieve the methods for working with snapshots:
PowerCLI C:> $CIVMView | Get-Member | >> Where-Object Name -like '*Snapshot*' TypeName: VMware.VimAutomation.Cloud.Views.Vm Name MemberType Definition ---- ---------- ---------- CreateSnapshot Method void CreateSnapshot(System... CreateSnapshot_Task Method VMware.VimAutomation.Cloud... GetSnapshotSection Method VMware.VimAutomation.Cloud... RemoveAllSnapshots Method void RemoveAllSnapshots() RemoveAllSnapshots_Task Method VMware.VimAutomation.Cloud... RevertToCurrentSnapshot Method void RevertToCurrentSnapsh... RevertToCurrentSnapshot_Task Method VMware.VimAutomation.Cloud...
As you can see in the preceding output, there are two methods to create snapshots. The CreateSnapshot()
method will create a snapshot and waits until the snapshot is created before it returns. The CreateSnapshot_Task()
method will create a task that creates the snapshot and will return right after creating the task. The method does not wait until the snapshot is created. Both methods have the following parameters:
System.Nullable[bool] memory
System.Nullable[bool] quiesce
string name
string description
We will now create a snapshot named Before patching
, and description Snapshot created before patching
. We will not snapshot the memory or quiesce the virtual machine:
PowerCLI C:> $CIVMView.CreateSnapshot($false,$false,
'Before patching','Snapshot created before patching')
The preceding command does not return any output.
In the output of the Get-Member
cmdlet in the preceding section, Creating Snapshots, you will see the only method related to snapshots with Get
in its name is GetSnapshotSection()
. We will use this method and see what it returns:
PowerCLI C:> $CIVMView.GetSnapshotSection() Href : https://p13v37-vcd.vchs.vmware.com/api/vApp/vm-fe0 d4f43-cb41-40f9-a068-9d7d9aec9e3f/snapshotSection Type : application/vnd.vmware.vcloud.snapshotSection+xml Link : Snapshot : VMware.VimAutomation.Cloud.Views.Snapshot Any : Required : False AnyAttr : {xsi:schemaLocation} Info : VMware.VimAutomation.Cloud.Views.OvfMsg Client : VMware.VimAutomation.Cloud.Views.CloudClient VCloudExtension :
The Snapshot
property looks like a candidate for further investigation:
PowerCLI C:> $CIVMView.GetSnapshotSection().Snapshot PoweredOn : False Created : 10/10/2016 5:24:28 PM Size : 21474836480 AnyAttr : VCloudExtension :
As you can see in the output of the preceding example, we can retrieve the powered on state, the creation date and time, and the size of the snapshot.
In the following example, we will list all snapshots and display the name of the vCloud virtual machine, if the machine was powered on while creating the snapshot and the memory of the virtual machine was included in the snapshot, the creation date and time of the snapshot, and the size of the snapshot. We use the -PipelineVariable
parameter to save the vCloud virtual machine object in the $CIVM
parameter. A calculated property is used to display the virtual machine name:
PowerCLI C:> (Get-CIVM -PipelineVariable $CIVM | >> Get-CIView).GetSnapshotSection().Snapshot | >> Select-Object -Property @{Name='VM';Expression={$CIVM.Name}}, >> PoweredOn,Created,Size VM PoweredOn Created Size -- --------- ------- ---- CentOS64-64BIT False 10/10/2016 5:24:28 PM 21474836480
If you want to discard the changes made in a virtual machine and revert to the current snapshot, you can use the following command:
PowerCLI C:> $CIVMView.RevertToCurrentSnapshot()
The preceding command does not return any output.
If you want to keep the changes made in a virtual machine and want to remove the snapshots, you can use the following command:
PowerCLI C:> $CIVMView.RemoveAllSnapshots()
The preceding command does not return any output.
To check whether the snapshots are removed, we can use the following command, used before in the Retrieving snapshots section:
PowerCLI C:> $CIVMView.GetSnapshotSection().Snapshot
If the preceding command does not return any output, you are sure that all snapshots of the cloud virtual machine are removed.
18.118.1.232