Pre-requisites
1) A Windows Server upon which to run the PowerShell
script (i.e. a SnapCenter Server).
2) Windows Server GPO needs to permit running scheduled
tasks.
3) DataONTAP PowerShell Toolkit needs to have been
installed on the server.
4) vsadmin SVM credentials (or a custom role - that
can delete snapshots - with application ‘ontapi’).
5) SVM management LIF.
6) The script below saved as say
C:\Scripts\SnapshotDeletor.ps1
7) A CSV saved as say C:\Scripts\SnapshotDeletor.csv
8) SVM credentials stored in the credentials cache using:
SnapshotDeletor.ps1
-SetupCredentials
Image: Example of the controlling CSV (SnapshotDeletor.CSV)
The CSV has the following columns:
- SVM Mgmt IP/FQDN
- SVM Name
- Volume
- Keep X Days Snaps
- Skip
Use ‘Y’ in the column ‘Skip’ if you want to skip a volume
in the list.
The Script
######################
## SNAPSHOT DELETOR ##
######################
# 1) Setup the SVM(s) credentials
# 2) Create a CSV with 5 columns:
# SVM Mgmt IP/FQDN,SVM Name,Volume,Keep X
Days Snaps,Skip
# 3) Provide path to the CSV
# (default path =
C:\SCRIPTS\SNAPDELETOR.CSV)
Param(
[Switch]$SetupCredentials,
[String]$SVMname, # used to setup creds
[String]$SVMmgmt, # used to setup creds
[String]$Username, # used to setup creds
[String]$Password, # used to setup creds
[String]$CSVPath = "C:\SCRIPTS\SnapshotDeletor.CSV",
[String]$LogPath = "C:\SCRIPTS\SnapshotDeletor.LOG"
)
Import-Module DataONTAP
################################
## SECTION: SETUP CREDENTIALS ##
################################
If($SetupCredentials){
Write-Host "Enter SVM Mgmt IP/FQDN: " -N
If($SVMmgmt){Write-Host $SVMmgmt}
else{$SVMmgmt = Read-Host}
Write-Host "Enter SVM Name: " -N
If($SVMname){Write-Host $SVMname}
else{$SVMname = Read-Host}
Write-Host "Enter SVM username: " -N
If($Username){Write-Host $Username}
else{$Username = Read-Host}
Write-Host "Enter SVM password: " -N
If($Password){
Write-Host "********"
$PwSecure = ConvertTo-SecureString -String $Password
}else{
$PwSecure = Read-Host -AsSecureString
}
$Credential = New-Object
System.Management.Automation.PsCredential($Username,$PwSecure)
Connect-NcController -Name $SVMmgmt -Credential $Credential -Vserver
$SVMname
Add-NcCredential -Name $SVMmgmt -Credential $Credential
EXIT
}
###############################
## SECTION: DELETE SNAPSHOTS ##
###############################
$CSV = Import-CSV $CSVPath
"START OF LOG" > $LogPath
$CSV | Foreach{
$SVMmgmt = $_."SVM Mgmt IP/FQDN"
$SVMname = $_."SVM Name"
$Volume = $_."Volume"
$Days = $_."Keep X Days
Snaps"
If($_.Skip -eq "Y"){
"$SVMname : $Volume : skipped!" >> $LogPath
}Else{
If($global:CurrentNcController){
If($global:CurrentNcController.Name -ne $SVMmgmt){
$global:CurrentNcController = $NULL
}
}
If($global:CurrentNcController){
If($global:CurrentNcController.Vserver -ne $SVMname){
$global:CurrentNcController =
$NULL
}
}
If(!$global:CurrentNcController){
Connect-NcController -Name $SVMmgmt -Vserver $SVMname
}
Get-NcSnapshot -Volume $Volume -Vserver $SVMname | where-object
{$_.Created -lt (Get-Date).AddDays(-$Days)} | Foreach{
$SnapName = $_.Name
"$SVMname : $Volume : Removing snapshot $SnapName" >>
$LogPath
Remove-NcSnapshot -VserverContext $SVMname -Volume $Volume -Snapshot
$SnapName -confirm:$false
}
}
}
Comments
Post a Comment