-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDRMM-HyperVSnapshotAge.ps1
46 lines (39 loc) · 1.4 KB
/
DRMM-HyperVSnapshotAge.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
function Write-DRMMAlert ($message) {
write-host '<-Start Result->'
write-host "Alert=$message"
write-host '<-End Result->'
}
function Write-DRMMStatus ($message) {
write-host '<-Start Result->'
write-host "STATUS=$message"
write-host '<-End Result->'
}
function Write-DRMMDiagnostic ($messages) {
write-host '<-Start Diagnostic->'
foreach ($Message in $Messages) { $Message }
write-host '<-End Diagnostic->'
}
$version = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").CurrentVersion
if ($Version -lt "6.3") {
write-DRMMAlert "Unsupported OS. Only Server 2012R2 and up are supported - exclude this server from the monitor."
exit 1
}
# Import the Hyper-V module
Import-Module Hyper-V
# Get all VMs on the host
$snapshots = Get-VM | Get-VMSnapshot | Where-Object { $_.CreationTime -lt (Get-Date).AddDays(-$env:SnapshotAge) -and $_.snapshottype -ne "Replica" -and $_.Name -notlike "Veeam Replica" }
# Iterate through the VMs
$SnapshotState = foreach ($Snapshot in $snapshots) {
[PSCustomObject]@{
VMName = $snapshot.vmname
'Creation Date' = $snapshot.CreationTime
Snapshotname = $snapshot.Name
}
}
if (!$SnapshotState) {
Write-DRMMStatus "No aged snaphots found"
} else {
Write-DRMMAlert "Found aged snapshots: $($SnapshotState.VMName -join ', ')"
Write-DRMMDiagnostic ($SnapshotState | fl)
Exit 1
}