-
Notifications
You must be signed in to change notification settings - Fork 0
/
Remove-UAASnapshots.ps1
327 lines (260 loc) · 14.1 KB
/
Remove-UAASnapshots.ps1
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<#
Created By: John Zetterman
Last Modified: 6/12/2019
#>
Function Get-UAASnapshots {
[CmdletBinding(DefaultParameterSetName="default")]
Param
(
[Parameter(Mandatory=$false, Position=0, ParameterSetName="default")]
[string]$Datacenter,
[Parameter(Mandatory=$true, Position=0, ParameterSetName="Specific VM")]
[Parameter(Mandatory=$true, Position=0, ParameterSetName="Specific Snapshot")]
[string[]]$VirtualMachine,
[Parameter(Mandatory=$true, Position=1, ParameterSetName="Specific Snapshot")]
[string]$SnapshotName,
[Parameter(Mandatory=$false, Position=1, ParameterSetName="default")]
[Parameter(Mandatory=$false, Position=1, ParameterSetName="Specific VM")]
[int]$OlderThan = 0
)
Write-Debug "Using ParameterSetName: $($PSCmdlet.ParameterSetName)"
switch ($PSCmdlet.ParameterSetName)
{
"Specific VM"
{
Write-Debug "Processing VM: $VirtualMachine"
$Snapshots = Get-VM -Name $VirtualMachine | Get-Snapshot | Where-Object {$_.Created -lt (Get-Date).AddDays(-$OlderThan)}
}
"Specific Snapshot"
{
Write-Debug "Processing Snapshot: $SnapshotName"
$Snapshots = Get-Snapshot -VM $VirtualMachine -Name $SnapshotName | Where-Object {$_.Created -lt (Get-Date).AddDays(-$OlderThan)}
}
default
{
if ($Datacenter)
{
$Snapshots = Get-Datacenter -Name $Datacenter | Get-VM | Get-Snapshot | Where-Object {$_.Created -lt (Get-Date).AddDays(-$OlderThan)}
}
else
{
$Snapshots = Get-VM | Get-Snapshot | Where-Object {$_.Created -lt (Get-Date).AddDays(-$OlderThan)}
}
}
}
Return $Snapshots
<#
.SYNOPSIS
Gets a list of existing snapshots from vCenter.
.DESCRIPTION
Returns a list of snapshots from vCenter. Results can be filtered by
vSphere Datacenter, Days Old, or VM Name.
.INPUTS
None. You cannot pipe objects to Get-UAASnapshots.
.OUTPUTS
VMware.VimAutomation.ViCore.Impl.V1.VirtualMachine.SnapshotImpl. Get-UAASnapshots returns a custom
VMware object containing the results of the function.
.EXAMPLE
PS> Get-UAASnapshots -Datacenter 'Anchorage Datacenter' -OlderThan 2 | Select VM, Name, Created
VM Name Created
-- ---- -------
AR-TEST AR-TEST_vm-117097_1 5/21/2019 11:36:48 AM
anc-licensing04 VM Snapshot 1%252f4%252f2019, 10:15:38 AM 1/4/2019 10:15:54 AM
Arctic Before License Manager 6/5/2017 6:57:46 AM
.EXAMPLE
PS> Get-UAASnapshots -SnapshotName AR-TEST | Select VM, Name, Created
VM Name Created
-- ---- -------
AR-TEST AR-TEST_vm-117097_1 5/21/2019 11:36:48 AM
.LINK
https://github.com/uaanchorage/Manage-vSphere-Snapshots
#>
}
Function Remove-UAASnapshots {
[CmdletBinding(DefaultParameterSetName="default", SupportsShouldProcess=$True)]
Param
(
[Parameter(Mandatory=$false, Position=0, ParameterSetName="default")]
[string]$Datacenter,
[Parameter(Mandatory=$true, Position=0, ParameterSetName="Specific VM")]
[Parameter(Mandatory=$true, Position=0, ParameterSetName="Specific Snapshot")]
[string]$VirtualMachine,
[Parameter(Mandatory=$true, Position=1, ParameterSetName="Specific Snapshot")]
[string]$SnapshotName,
[Parameter(Mandatory=$true, Position=1, ParameterSetName="default")]
[Parameter(Mandatory=$true, Position=1, ParameterSetName="Specific VM")]
[int]$OlderThan,
[Parameter(Mandatory=$false, Position=2, ParameterSetName="default")]
[Parameter(Mandatory=$false, Position=2, ParameterSetName="Specific VM")]
[switch]$RemoveChildren,
[Parameter(Mandatory=$false, Position=3, ParameterSetName="default")]
[Parameter(Mandatory=$false, Position=3, ParameterSetName="Specific VM")]
[Parameter(Mandatory=$false, Position=3, ParameterSetName="Specific Snapshot")]
[switch]$EmailNotification
)
Write-Debug "Using ParameterSetName: $($PSCmdlet.ParameterSetName)"
$RemovedSnapshots = @()
switch ($PSCmdlet.ParameterSetName)
{
"Specific VM"
{
if ($RemoveChildren)
{
$Snapshots = Get-UAASnapshots -SnapshotName $VirtualMachine -OlderThan $OlderThan | Where-Object {$null -eq $_.Parent}
ForEach ($Snapshot in $Snapshots)
{
$RemovedObjectList = New-Object psobject
$RemovedObjectList | Add-Member -MemberType NoteProperty -Name VirtualMachine -Value $Snapshot.VM
$RemovedObjectList | Add-Member -MemberType NoteProperty -Name SnapshotName -Value $Snapshot.Name
$RemovedObjectList | Add-Member -MemberType NoteProperty -Name ChildSnapshots -Value $true
Write-Debug "VM Name: $($Snapshot.VirtualMachine)"
Write-Debug "Snapshot Name: $($Snapshot.Name)"
Write-Debug "Child Snapshots: Yes"
$Snapshot | Remove-Snapshot -RemoveChildren -Confirm:$false
$RemovedSnapshots += $RemovedObjectList
}
}
else
{
$Snapshots = Get-UAASnapshots -SnapshotName $VirtualMachine -OlderThan $OlderThan | Where-Object {$null -eq $_.Parent}
ForEach ($Snapshot in $Snapshots)
{
$RemovedObjectList = New-Object psobject
$RemovedObjectList | Add-Member -MemberType NoteProperty -Name VirtualMachine -Value $Snapshot.VM
$RemovedObjectList | Add-Member -MemberType NoteProperty -Name SnapshotName -Value $Snapshot.Name
$RemovedObjectList | Add-Member -MemberType NoteProperty -Name ChildSnapshots -Value $false
Write-Debug "VM Name: $($Snapshot.VirtualMachine)"
Write-Debug "Snapshot Name: $($Snapshot.Name)"
Write-Debug "Child Snapshots: No"
$Snapshot | Remove-Snapshot -Confirm:$false
$RemovedSnapshots += $RemovedObjectList
}
}
}
"Specific Snapshot"
{
$Snapshot = Get-UAASnapshots -VirtualMachine $VirtualMachine -SnapshotName $SnapshotName
$RemovedObjectList = New-Object psobject
$RemovedObjectList | Add-Member -MemberType NoteProperty -Name VirtualMachine -Value $Snapshot.$VM
$RemovedObjectList | Add-Member -MemberType NoteProperty -Name SnapshotName -Value $Snapshot.Name
$RemovedObjectList | Add-Member -MemberType NoteProperty -Name ChildSnapshots -Value $false
Write-Debug "VM Name: $($Snapshot.VirtualMachine)"
Write-Debug "Snapshot Name: $($Snapshot.SnapshotName)"
Write-Debug "Child Snapshots: No"
$Snapshot | Remove-Snapshot -Confirm:$false
$RemovedSnapshots += $RemovedObjectList
}
default
{
if ($Datacenter -and $RemoveChildren)
{
$Snapshots = Get-UAASnapshots -Datacenter $Datacenter -OlderThan $OlderThan | Where-Object {$null -eq $_.Parent}
ForEach ($Snapshot in $Snapshots)
{
$RemovedObjectList = New-Object psobject
$RemovedObjectList | Add-Member -MemberType NoteProperty -Name VirtualMachine -Value $Snapshot.VM
$RemovedObjectList | Add-Member -MemberType NoteProperty -Name SnapshotName -Value $Snapshot.Name
$RemovedObjectList | Add-Member -MemberType NoteProperty -Name ChildSnapshots -Value $true
$RemovedObjectList | Add-Member -MemberType NoteProperty -Name Created -Value $Snapshot.Created
Write-Debug "VM Name: $($Snapshot.VirtualMachine)"
Write-Debug "Snapshot Name: $($Snapshot.Name)"
Write-Debug "Child Snapshots: Yes"
$Snapshot | Remove-Snapshot -RemoveChildren -Confirm:$false
$RemovedSnapshots += $RemovedObjectList
}
}
elseif ($Datacenter)
{
$Snapshots = Get-UAASnapshots -Datacenter $Datacenter -OlderThan $OlderThan | Where-Object {$null -eq $_.Parent}
ForEach ($Snapshot in $Snapshots)
{
$RemovedObjectList = New-Object psobject
$RemovedObjectList | Add-Member -MemberType NoteProperty -Name VirtualMachine -Value $Snapshot.VM
$RemovedObjectList | Add-Member -MemberType NoteProperty -Name SnapshotName -Value $Snapshot.Name
$RemovedObjectList | Add-Member -MemberType NoteProperty -Name ChildSnapshots -Value $false
Write-Debug "VM Name: $($Snapshot.VirtualMachine)"
Write-Debug "Snapshot Name: $($Snapshot.Name)"
Write-Debug "Child Snapshots: No"
$Snapshot | Remove-Snapshot -Confirm:$false
$RemovedSnapshots += $RemovedObjectList
}
}
elseif ($RemoveChildren)
{
$Snapshots = Get-UAASnapshots -OlderThan $OlderThan | Where-Object {$null -eq $_.Parent}
ForEach ($Snapshot in $Snapshots)
{
$RemovedObjectList = New-Object psobject
$RemovedObjectList | Add-Member -MemberType NoteProperty -Name VirtualMachine -Value $Snapshot.VM
$RemovedObjectList | Add-Member -MemberType NoteProperty -Name SnapshotName -Value $Snapshot.Name
$RemovedObjectList | Add-Member -MemberType NoteProperty -Name ChildSnapshots -Value $true
Write-Debug "VM Name: $($Snapshot.VirtualMachine)"
Write-Debug "Snapshot Name: $($Snapshot.Name)"
Write-Debug "Child Snapshots: Yes"
$Snapshot | Remove-Snapshot -RemoveChildren -Confirm:$false
$RemovedSnapshots += $RemovedObjectList
}
}
else
{
$Snapshots = Get-UAASnapshots -OlderThan $OlderThan | Where-Object {$null -eq $_.Parent}
ForEach ($Snapshot in $Snapshots)
{
$RemovedObjectList = New-Object psobject
$RemovedObjectList | Add-Member -MemberType NoteProperty -Name VirtualMachine -Value $Snapshot.VM
$RemovedObjectList | Add-Member -MemberType NoteProperty -Name SnapshotName -Value $Snapshot.Name
$RemovedObjectList | Add-Member -MemberType NoteProperty -Name ChildSnapshots -Value $false
Write-Debug "VM Name: $($Snapshot.VirtualMachine)"
Write-Debug "Snapshot Name: $($Snapshot.Name)"
Write-Debug "Child Snapshots: No"
$Snapshot | Remove-Snapshot -Confirm:$false
$RemovedSnapshots += $RemovedObjectList
}
}
}
}
$css = @"
<STYLE>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #34495e; color:#ffffff;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
TR:Nth-Child(Even) {Background-Color: #dddddd;}
</STYLE>
"@
if ($EmailNotification -and ($null -ne $RemovedSnapshots))
{
$EmailBody = $RemovedSnapshots | ConvertTo-Html -Head $css -PreContent "Below is a summary of the snapshots that were removed from vCenter during script execution:<br /><br />" -PostContent "<br />File Name: $($MyInvocation.MyCommand). <br />Execution completed at $(Get-Date)" | Out-String
Send-MailMessage -To '[email protected]' -From '[email protected]' -Subject 'vCenter Snapshot Removal Summary' -Body $EmailBody -BodyAsHtml -SmtpServer 'aspam-out.uaa.alaska.edu'
}
else
{
$EmailBody = $RemovedSnapshots | ConvertTo-Html -PreContent "There were no snapshots detected.<br /><br />" -PostContent "<br />File Name: $($MyInvocation.MyCommand). <br />Execution completed at $(Get-Date)" | Out-String
Send-MailMessage -To '[email protected]' -From '[email protected]' -Subject 'vCenter Snapshot Removal Summary' -Body $EmailBody -BodyAsHtml -SmtpServer 'aspam-out.uaa.alaska.edu'
}
Return $RemovedSnapshots
<#
.SYNOPSIS
Remove a list of existing snapshots from vCenter.
.DESCRIPTION
Returns a list of snapshots that were removed from vCenter. Results can be
filtered by vSphere Datacenter, Days Old, VM Name, or Snapshot Name.
.INPUTS
None. You cannot pipe objects to Remove-UAASnapshots.
.OUTPUTS
System.Array. Get-UAASnapshots returns an array of objects containing
the results of the function.
.EXAMPLE
PS C:\Users\jmzetterman.UA\Documents\OneDrive Personal\OneDrive\UAAGit> Remove-UAASnapshots -VirtualMachine AR-TEST -SnapshotName AR-TEST_vm-117097_1
Name State % Complete Start Time Finish Time
---- ----- ---------- ---------- -----------
RemoveSnapshot_Task Completed 100 10:32:27 AM 10:32:40 AM
.EXAMPLE
PS> Remove-UAASnapshots -Datacenter 'Anchorage Datacenter' -OlderThan 0 -RemoveChildren
VirtualMachine SnapshotName ChildSnapshots Created
-------------- ------------ -------------- -------
anc-vm01 VM Snapshot 1 True 7/10/2017 1:23:22 AM
anc-vm02 VM Snapshot 1 True 6/8/2018 3:53:51 PM
.LINK
https://github.com/uaanchorage/Manage-vSphere-Snapshots
#>
}