From 8694c402108ac7a67f5cc5ad6feac4c74b4ff18b Mon Sep 17 00:00:00 2001 From: William Lam Date: Mon, 30 Apr 2018 05:22:46 -0700 Subject: [PATCH] Parentless Instant Clone for vSphere 6.7 --- Modules/InstantClone/InstantClone.psm1 | 68 ++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 Modules/InstantClone/InstantClone.psm1 diff --git a/Modules/InstantClone/InstantClone.psm1 b/Modules/InstantClone/InstantClone.psm1 new file mode 100644 index 0000000..bd36545 --- /dev/null +++ b/Modules/InstantClone/InstantClone.psm1 @@ -0,0 +1,68 @@ +Function New-InstantClone { +<# + .NOTES + =========================================================================== + Created by: William Lam + Date: Apr 29, 2018 + Organization: VMware + Blog: www.virtuallyghetto.com + Twitter: @lamw + =========================================================================== + .SYNOPSIS + This function demonstrates the use of the new "Parentless" Instant Clone + API that was introduced in vSphere 6.7 + .DESCRIPTION + Function to create new "Parentless" Instant Clones in vSphere 6.7 + .EXAMPLE + $SourceVM = "Foo" + $newVMName = Foo-IC-1 + $guestCustomizationValues = @{ + "guestinfo.ic.hostname" = $newVMName + "guestinfo.ic.ipaddress" = "192.168.30.10" + "guestinfo.ic.netmask" = "255.255.255.0" + "guestinfo.ic.gateway" = "192.168.30.1" + "guestinfo.ic.dns" = "192.168.30.1" + } + New-InstantClone -SourceVM $SourceVM -DestinationVM $newVMName -CustomizationFields $guestCustomizationValues + .NOTES + Make sure that you have both a vSphere 6.7 env (VC/ESXi) as well as + as the latest PowerCLI 10.1 installed which is reuqired to use vSphere 6.7 APIs +#> + param( + [Parameter(Mandatory=$true)][String]$SourceVM, + [Parameter(Mandatory=$true)][String]$DestinationVM, + [Parameter(Mandatory=$true)][Hashtable]$CustomizationFields + ) + $vm = Get-VM -Name $SourceVM + + $config = @() + $CustomizationFields.GetEnumerator() | Foreach-Object { + $optionValue = New-Object VMware.Vim.OptionValue + $optionValue.Key = $_.Key + $optionValue.Value = $_.Value + $config += $optionValue + } + + # SourceVM must either be running or running but in Frozen State + if($vm.PowerState -ne "PoweredOn") { + Write-Host -ForegroundColor Red "Instant Cloning is only supported on a PoweredOn or Frozen VM" + break + } + + # SourceVM == Powered On + if((Get-VM $SourceVM).ExtensionData.Runtime.InstantCloneFrozen -eq $false) { + Write-Host -ForegroundColor Red "Instant Cloning from a PoweredOn VM has not been implemented" + break + } + + $spec = New-Object VMware.Vim.VirtualMachineInstantCloneSpec + $locationSpec = New-Object VMware.Vim.VirtualMachineRelocateSpec + $spec.Config = $config + $spec.Location = $locationSpec + $spec.Name = $DestinationVM + + Write-Host "Creating Instant Clone $DestinationVM ..." + $task = $vm.ExtensionData.InstantClone_Task($spec) + $task1 = Get-Task -Id ("Task-$($task.value)") + $task1 | Wait-Task | Out-Null +} \ No newline at end of file