-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckForVMwareSnapshots.ps1
79 lines (71 loc) · 3.47 KB
/
CheckForVMwareSnapshots.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
<#
.SYNOPSIS
Lists all present snapshots older than a specified amount of days.
.DESCRIPTION
Lists all present snapshots for a given VMware vSphere.
It shows for each snapshot the associated virtual machine name, name and description and the snapshot's age in days.
IMPORTANT: Requires VMware PowerCLI be installed & other dependencies to be installed.
Check all the followings links carefully:
http://thesolving.com/virtualization/how-to-install-and-configure-vmware-powercli-version-10/
https://stackoverflow.com/questions/51406685/how-do-i-install-the-nuget-provider-for-powershell-on-a-unconnected-machine-so-i
https://kb.paessler.com/en/topic/60932-prtgshell-module-not-loaded:-ensure-the-module-is-visible-for-32-bit-powershell
.PARAMETER Server
The name or ip address of the host or vCenter Server
.PARAMETER Username
Name of the administrative account needed to authenticate to vSphere.
.PARAMETER Password
The password for the given user.
.PARAMETER IgnoreVMNamesStartingWith
If specified, snapshots associated to Virtual Machine names beginning with this parameter (e.g. "replica") will be filtered from the result.
If no parameter is specified or the string is empty, all snapshots (matching the MinAgeInDays criteria) will be included in the result.
.PARAMETER MinAgeInDays
Only list snapshots with an age of at least x days. If the value is 0, the age is not considered in the result.
.EXAMPLE
.\CheckForPresentVMwareSnapshots.ps1 -Server %host -Username "MyUsername" -Password "MyPassword" -IgnoreVMNamesStartingWith = "replica" -MinAgeInDays 3
Values with % will be replaced by PRTG automatically.
.NOTES
Author: Antoine Hauck
Company: Keynet AG | (c) 2018
.LINK
http://www.keynet.ch
#>
param(
[string]$Server = "vc01.domain.com",
[string]$Username = "account@domain.com",
[string]$Password = "SecurePassword",
[string]$IgnoreVMNamesStartingWith = "replica",
[int]$MinAgeInDays = 0
)
Try
{
$conn = Connect-VIServer $Server -Username $Username -Password $Password -Force
$Snapshots = Get-VM | Get-Snapshot | Select VM, Name,Description,@{Name="SizeGB";Expression={ [math]::Round($_.SizeGB,2) }},@{Name="Creator";Expression={ Get-SnapshotCreator -VM $_.VM -Created $_.Created }},Created,@{Name="Days";Expression={ (New-TimeSpan -End (Get-Date) -Start $_.Created).Days }} | where { $_.Days -ge $MinAgeInDays }
$SnapshotsFiltered = @()
Foreach ($Snapshot in $Snapshots)
{
If($IgnoreVMNamesStartingWith.Length -gt 0 -and $Snapshot.VM.ToString().StartsWith($IgnoreVMNamesStartingWith))
{
# Snapshot starts with ignored name. Skip entry.
} else {
$SnapshotsFiltered += $Snapshot
}
}
Disconnect-VIServer $conn -Force -Confirm:$false
if($SnapshotsFiltered.Length -eq 0)
{
Write-Host "0:No snapshots found with an age of at least $MinAgeInDays days."
exit 0
}
$Result = $SnapshotsFiltered.Length.ToString() + ":"
Foreach ($Snapshot in $SnapshotsFiltered)
{
$Result += "VM:" + $Snapshot.VM + ", Name:" + $Snapshot.Name + ", Description:" + $Snapshot.Description + ", Days: " + $Snapshot.Days + " ---- "
}
Write-Host $Result
exit 1
}
catch
{
Write-Host "-1:Error occurred while checking for snapshots:"$_.Exception.GetType().FullName, $_.Exception.Message. $_.Exception.Stacktrace
exit 1
}