Nutanix

Cleanup Nutanix Snapshots After # Amount Of Days

1 min read

Quick blog post. The PowerShell script is self explaining 😉

<#
    This script checks the Nutanix cluster for available snapshots (Protection Domain snapshots are not part of the check).
    The list of snapshots is checked against the $DeleteAfterDays and is the snapshot is older then the given days it will be deleted.
 
    Cleaned up space is freed after curator has done his magics ;)
 
    Please change $DeleteAfterdays, $DryRun, $PrismElementIP, $UserName, $Password to match your needs.
    The $DryRun can be $True or $False. When it is $True no real snapshots will be deleted.
    Remove the "-password" in the connect string and the script will ask for you password. ;)
 
    Make sure the powershell cmdlets are installed. More info here: https://portal.nutanix.com/page/documents/details?targetId=PS-Cmdlets-AOS-v6_5:ps-ps-cmdlets-installation-v2.html
 
    This script is an evolution of https://next.nutanix.com/how-it-works-22/list-current-snapshots-via-a-script-32693
 
    Version    : 1.0 
    Date       : 23 December 2022
    Created by : Jeroen Tielen - Tielen Consultancy B.V.
    Email      : jeroen@tielenconsultancy.nl
 
    History    : 1.0 : 23 December 2022 - Initial setup script.
#>

# Tune this variables to your need.
$DeleteAfterDays    = 30
$Dryrun             = $True
$PrismElementIP     = "192.168.2.76"
$UserName           = "admin"
$Password           = ConvertTo-SecureString "nutanix/4u" -AsPlainText -Force
 
# Load snapins and connect to cluster.
ASNP Nutanix*
Connect-NutanixCluster -Server $PrismElementIP -UserName $UserName -Password $Password -AcceptInvalidSSLCerts -ForcedConnection
 
# Script Start.
$NutSnaps = Get-NTNXSnapshot
$AllNutVMs = Get-NTNXVM | Select UUID, VMName
 
If ($DryRun -eq $True) {
    Write-Host " Dryrun activated, no snapshots will be deleted." -ForegroundColor Yellow -BackgroundColor Black 
} else {
    Write-Host " Dryrun disabled, snapshots will be deleted if older then $DeleteAfterdays days.`n" -ForegroundColor Red -BackgroundColor Black
}
 
ForEach ($Snap in $NutSnaps) {
    $VMName = ($AllNutVMs | Where { $_.UUID -eq $Snap.VMUUID }).VMName
    $SnapTime = $Snap.createdtime / 1000
    $SnapDt = (Get-Date '1/1/1970').AddMilliSeconds($SnapTime)
    $ActualSnapDt = $SnapDt.ToLocalTime()
    $Limit = (Get-Date).AddDays(-$DeleteAfterDays)
    If ($Limit -lt $ActualSnapDt) {
        Write-Host " Snapshot named " -NoNewline
        Write-Host $Snap.SnapShotName -ForegroundColor Yellow -NoNewline
        Write-Host " on VM " -NoNewline
        Write-Host $VMName -ForegroundColor Yellow -NoNewline
        Write-Host " created on " -NoNewline
        Write-Host $ActualSnapDt -ForegroundColor Yellow -NoNewline
        Write-Host " is within the limit and will not be deleted."
    } Else {
        Write-Host " Snapshot named " -NoNewline
        Write-Host $Snap.SnapShotName -ForegroundColor Red -NoNewline
        Write-Host " on VM " -NoNewline
        Write-Host $VMName -ForegroundColor Red -NoNewline
        Write-Host " created on " -NoNewline
        Write-Host $ActualSnapDt -ForegroundColor Red -NoNewline
        Write-Host " is older then the limit and will be deleted."
        If ($DryRun -eq $False) { $Remove = Remove-NTNXSnapshot -UUID $Snap.UUID }
    }
}

Discover more from Jeroen Tielen

Subscribe to get the latest posts sent to your email.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.