VMwareCloneVMFromSnapshot.ps1 4.72 KB
Newer Older
Michael Kleger's avatar
Michael Kleger committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
Function New-VMFromSnapshot {
<#
 .SYNOPSIS
 Function to create a clone from a snapshot of a VM.

 .DESCRIPTION
 Function to create a clone from a snapshot of a VM.

 .PARAMETER SourceVM
 VM to clone from.

.PARAMETER CloneName
 Name of the clone to create

.PARAMETER SnapshotName
 Name of the snapshot to clone from

.PARAMETER CurrentSnapshot
 Use the current snapshot instead of a named snapshot

.PARAMETER Cluster
 Name of the cluster to place the clone in

.PARAMETER Datastore
 Name of the datastore to place the clone in

.PARAMETER VMFolder
 Name of the Virtual Machine folder to put the VM in

.PARAMETER LinkedClone
 Create a linked clone from the snapshot, rather than a full clone

.INPUTS
 String.
 System.Management.Automation.PSObject.

.OUTPUTS
 VMware.Vim.ManagedObjectReference.

.EXAMPLE
 PS> New-VMFromSnapshot -SourceVM VM01 -CloneName "Clone01" -Cluster "Test Cluster" -Datastore "Datastore01"

.EXAMPLE
 PS> New-VMFromSnapshot -SourceVM VM01 -CloneName "Clone01" -SnapshotName "Testing" -Cluster "Test Cluster" -Datastore "Datastore01" -VMFolder "Test Clones" -LinkedClone

#>
[CmdletBinding(DefaultParameterSetName=Current Snapshot)][OutputType('VMware.Vim.ManagedObjectReference')]

Param
 (

[parameter(Mandatory=$true)]
 [ValidateNotNullOrEmpty()]
 [PSObject]$SourceVM,

 [parameter(Mandatory=$true)]
 [ValidateNotNullOrEmpty()]
 [String]$CloneName,

[parameter(Mandatory=$true,ParameterSetName="Named Snapshot")]
 [ValidateNotNullOrEmpty()]
 [String]$SnapshotName,

[parameter(Mandatory=$false)]
 [ValidateNotNullOrEmpty()]
 [String]$Cluster,

[parameter(Mandatory=$false)]
 [ValidateNotNullOrEmpty()]
 [String]$Datastore,

[parameter(Mandatory=$false)]
 [ValidateNotNullOrEmpty()]
 [String]$VMFolder,

[parameter(Mandatory=$false)]
 [ValidateNotNullOrEmpty()]
 [Switch]$LinkedClone
 )

# --- Retrieve snapshot tree using try / catch since if it doesn't exist, an exception is generated
 function Test-SnapshotExists ($SnapshotQuery) {

try {
 Write-Verbose "Testing $SnapshotQuery....`n"
 $TestSnapshot = Invoke-Expression $SnapshotQuery
 Write-Output $TestSnapshot
 }

catch [Exception]{

$TestSnapshot = $false
 Write-Output $TestSnapshot
 }
 }

try {

if ($SourceVM.GetType().Name -eq "string"){

 try {
 $SourceVM = Get-VM $SourceVM -ErrorAction Stop
 }
 catch [Exception]{
 Write-Warning "VM $SourceVM does not exist"
 }
 }

 elseif ($SourceVM -isnot [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl]){
 Write-Warning "You did not pass a string or a VM object"
 Return
 }

 # --- Set values for the Clone Spec
 if ($PSBoundParameters.ContainsKey('Cluster')){

 $DefaultClusterResourcePoolMoRef = (Get-Cluster $Cluster | Get-ResourcePool "Resources").ExtensionData.MoRef
 }

if ($PSBoundParameters.ContainsKey('Datastore')){

$DatastoreMoRef = (Get-Datastore $Datastore).ExtensionData.MoRef
 }

if ($PSBoundParameters.ContainsKey('LinkedClone')){

$CloneType = "createNewChildDiskBacking"
 }
 else {

$CloneType = "moveAllDiskBackingsAndDisallowSharing"
 }

if ($PSBoundParameters.ContainsKey('VMFolder')){

try {

$Folder = Get-Folder $VMFolder -Type VM -ErrorAction Stop
 $CloneFolder = $Folder.ExtensionData.MoRef
 }
 catch [Exception] {

Write-Warning "VM Folder $VMFolder does not exist, using existing folder instead"
 $CloneFolder = $SourceVM.ExtensionData.Parent
 }
 }
 else {

$CloneFolder = $SourceVM.ExtensionData.Parent
 }

# --- Create CloneSpec and initiate Clone Task
 switch ($PsCmdlet.ParameterSetName)
 {
 "Named Snapshot" {

 $Snapshots = @()
 $SnapshotQuery = '$SourceVM.ExtensionData.Snapshot.RootSnapshotList[0]'

while ($Snapshot = Test-SnapshotExists -SnapshotQuery $SnapshotQuery){

$SnapshotQuery += '.ChildSnapshotList[0]'
 $Snapshots += $Snapshot
 }

$CloneSpec = New-Object Vmware.Vim.VirtualMachineCloneSpec
 $CloneSpec.Snapshot = ($Snapshots | Where-Object {$_.Name -eq $SnapshotName}).Snapshot
 $CloneSpec.Location = New-Object Vmware.Vim.VirtualMachineRelocateSpec
 $CloneSpec.Location.Pool = $DefaultClusterResourcePoolMoRef
 $CloneSpec.Location.Datastore = $DatastoreMoRef
 $CloneSpec.Location.DiskMoveType = [Vmware.Vim.VirtualMachineRelocateDiskMoveOptions]::$CloneType

$SourceVM.ExtensionData.CloneVM_Task($CloneFolder, $CloneName, $CloneSpec)
 }

"Current Snapshot" {

$CloneSpec = New-Object Vmware.Vim.VirtualMachineCloneSpec
 $CloneSpec.Snapshot = $SourceVM.ExtensionData.Snapshot.CurrentSnapshot
 $CloneSpec.Location = New-Object Vmware.Vim.VirtualMachineRelocateSpec
 $CloneSpec.Location.Pool = $DefaultClusterResourcePoolMoRef
 $CloneSpec.Location.Datastore = $DatastoreMoRef
 $CloneSpec.Location.DiskMoveType = [Vmware.Vim.VirtualMachineRelocateDiskMoveOptions]::$CloneType

$SourceVM.ExtensionData.CloneVM_Task($CloneFolder, $CloneName, $CloneSpec)
 }
 }
 }
 catch [Exception]{

 throw "Unable to deploy new VM from snapshot"
 }
}