From 7e112ae44a25473464bbc4fb862c5b6329d1b991 Mon Sep 17 00:00:00 2001 From: Athreyasubash <121850427+Athreyasubash@users.noreply.github.com> Date: Tue, 23 Jan 2024 23:14:13 +0530 Subject: [PATCH] Create Delete Snapshots after X Days --- .../Delete Snapshots after X Days | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 NFS Snapshot Management/Delete Snapshots after X Days diff --git a/NFS Snapshot Management/Delete Snapshots after X Days b/NFS Snapshot Management/Delete Snapshots after X Days new file mode 100644 index 00000000..5a408dcd --- /dev/null +++ b/NFS Snapshot Management/Delete Snapshots after X Days @@ -0,0 +1,36 @@ +##PowerShell Script to delete snapshots + +Connect-AzAccount +Set-AzContext -Subscription "Subscription Name" + +$resourceGroupName = "Resouce group Name" +$accountName = "Storage account name" +$shareName = "Share Name" + +$snapshotLifetimeInDays = "15" +#$snapshotLifetimeInMinutes = "" +$whatIf = $false + +$CurrentDateTime = (Get-Date).ToUniversalTime() +$DateTimeThreshold = $CurrentDateTime.AddDays(-$snapshotLifetimeInDays) +#$DateTimeThreshold = $CurrentDateTime.AddMinutes(-$snapshotLifetimeInMinutes) + +Write-Host "Querying all snapshots for share '$shareName'" +$snapshotList = (Get-AzRmStorageShare -ResourceGroupName $resourcegroupname -StorageAccountName $accountName -IncludeSnapshot) | Where-Object { $_.Name -eq $shareName -and $_.snapshotTime -ne $null } +$snapshotList + +Write-Host "Current date/time is $CurrentDateTime. Removing share snapshots older than '$DateTimeThreshold'" +foreach ($snapshot in $snapshotList) { + if ($snapshot.SnapshotTime -lt $DateTimeThreshold) { + Write-Host "Removing snapshot '$($snapshot.snapshotTime)' of share '$($snapshot.Name)'" + if ($whatIf -ne $true) { +Remove-AzRmStorageShare -ResourceGroupName $resourcegroupname -StorageAccountName $accountName -Name $snapshot.Name -snapshotTime $snapshot.snapshotTime -Force + } + } else { + Write-Host "Retaining recent snapshot '$($snapshot.snapshotTime)' of share '$($snapshot.Name)'" + } +} + +Write-Host "Querying all snapshots for share '$shareName' after deletion of old snapshots" +$snapshotListNew = (Get-AzRmStorageShare -ResourceGroupName $resourcegroupname -StorageAccountName $accountName -IncludeSnapshot) +$snapshotListNew