-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-StaleSnaps.ps1
133 lines (122 loc) · 5.99 KB
/
Get-StaleSnaps.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
param(
[switch]$Delete,
[switch]$Eradicate
)
<#
NAME: Get-StaleSnaps.ps1
AUTHOR: Jake Dennis
DATE : 6/4/2019
DESCRIPTION
This script will return snapshots older than a user-given threshold in days. This script is dependent on valid credentials to a pure array and a local text file with all the arrays in the environment.
EXAMPLE
PS H:\> C:\Users\Documents\PureSDK\Get-StaleSnaps.ps1 -delete
Please enter the maximum acceptable age of a snapshot in days: 14
Filtering for snapshots older than 14 days.
=========================================================================
puredev1
=========================================================================
volumename1.snapshot40 - 62.19 GB - 40.86 days
(...)
volumename13.snapshot14 - 3.09 GB - 14.11 days
There are 13 snapshot(s) older than 14 days consuming a total of 1093 GB on the array.
LINK
https://github.com/JakeDennis/PureStorageAdmin
#>
Import-Module -Name PureStoragePowerShellSDK
Get-Module -Name PureStoragePowerShellSDK
#Set email parameters
$Email = @{
From = "Pure Storage Health Check <[email protected]>"
To = @("[email protected]")
Subject = "[Pure] Stale Snapshot Report"
SMTPServer = "[email protected]"
Body = $EmailBody
}
Function Get-StaleSnaps{
param(
[switch]$Delete,
[switch]$Eradicate
)
#Establish variables, Pure's time format, and gather current time.
$1GB = 1024*1024*1024
$CurrentTime = Get-Date
$DateTimeFormat = 'yyyy-MM-ddTHH:mm:ssZ'
[int]$SnapAgeThreshold = 15
$Arrays = Get-Content 'D:\StorageScripts\Arrays.txt'
#Credentials for scheduled use
$User = 'svc.pure_collector'
try{
$Pass = Get-Content D:\StorageScripts\Password.txt | ConvertTo-SecureString
$Creds = New-Object System.Management.Automation.PSCredential ($User, $Pass)
}
catch{
Write-Host ""
Write-Host "Error processing credentials." -ForegroundColor Yellow
Write-Host "If credentials do not exist in $($PWD),
consider changing your working directory or
creating the file to store your credentials using the following command." -ForegroundColor Yellow
Write-Host ""
Write-Host "Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File '.\Password.txt'" -ForegroundColor Cyan
Exit
}
#Get all arrays needing to be queried from a local text file. Establish and reset counter variables.
[int]$SpaceConsumedTotal = 0
[int]$SnapNumberTotal = 0
foreach($FlashArrayID in $Arrays){
$Timespan = $null
[int]$SpaceConsumed = 0
[int]$SnapNumber = 0
try{
$FlashArray = New-PfaArray -EndPoint $FlashArrayID -Credentials $Creds -IgnoreCertificateError -HttpTimeOutInMilliSeconds 15000
$Snapshots = Get-PfaAllVolumeSnapshots -Array $FlashArray
Write-Output ""
Write-Output "========================================================================="
Write-Output " $FlashArrayID "
Write-Output "========================================================================="
}
catch{
Write-Host "Error processing $($FlashArrayID) with $($User)."
}
#Get all snapshots and compute the age of them. $DateTimeFormat variable taken from above; this is needed in order to parse Pure's time format.
foreach($Snapshot in $Snapshots){
$SnapshotDateTime = $Snapshot.created
$SnapshotDateTime = [datetime]::ParseExact($SnapshotDateTime,$DateTimeFormat,$null)
$Timespan = New-TimeSpan -Start $SnapshotDateTime -End $CurrentTime
$SnapAge = $($Timespan.Days + $($Timespan.Hours/24) + $($Timespan.Minutes/1440))
$SnapAge = [math]::Round($SnapAge,2)
#Find snaps older than given threshold and output with formatted data.
if($SnapAge -gt $SnapAgeThreshold){
$SnapStats = Get-PfaSnapshotSpaceMetrics -Array $FlashArray -Name $Snapshot.name
$SnapSize = [math]::round($($SnapStats.total/$1GB),2)
$SpaceConsumed = $SpaceConsumed + $SnapSize
$SnapNumber = $SnapNumber + 1
#Delete snapshots
if($Delete -eq $true -and $Eradicate -eq $true){
Remove-PfaVolumeOrSnapshot -Array $FlashArray -Name $Snapshot.name -Eradicate
Write-Output "Eradicating $($Snapshot.name) - $($SnapSize) GB."
}
elseif($Delete -eq $true){
Remove-PfaVolumeOrSnapshot -Array $FlashArray -Name $Snapshot.name
Write-Output "Deleting $($Snapshot.name) - $($SnapSize) GB."
}
else {
Write-Output $Snapshot.name
Write-Output " $SnapSize GB"
Write-Output " $SnapAge days"
}
}
}
#Display final message for array results.
Write-Output "There are $($SnapNumber) snapshot(s) older than $($SnapAgeThreshold) days consuming a total of $($SpaceConsumed) GB on the array."
Disconnect-PfaArray -Array $FlashArray
$SnapNumberTotal = $SnapNumberTotal + $SnapNumber
$SpaceConsumedTotal = $SpaceConsumedTotal + $SpaceConsumed
}
Write-Output "There are $($SnapNumberTotal) snapshot(s) older than $($SnapAgeThreshold) days consuming a total of $($SpaceConsumedTotal) GB for $($Arrays.Count) Arrays."
}
Get-StaleSnaps -Delete:$Delete -Eradicate:$Eradicate
#Output function to string variable for email body
#$EmailBody = Get-StaleSnaps | Out-String
#$EmailBody
#Send email; Must be sent as plain text in current format
#Send-MailMessage @Email