Nutanix : Script for reporting All VMs Snapshots from Nutanix AHV

Nutanix has deprecated the v1 PowerShell Cmdlets, which included over 200 essential commands like Get-NTNXSnapshot, which helps list snapshots in a cluster.

Manually tracking snapshots is time-consuming and increases the risk of errors. Fortunately, there’s an API request that can help us streamline this process.

To simplify this process, we can use a PowerShell script that automates Nutanix snapshot reporting. This script connects to the Nutanix REST API to retrieve snapshot details and generates a well-formatted report, which can be sent via email for quick and easy monitoring.

Prerequisites

Before running the script, ensure that:

  • You have access to the Nutanix Prism API.
  • The necessary credentials for authentication are available.
  • PowerShell is installed on your system.
  • SMTP is configured to send emails.

Step 1: Define Connection & Email Parameters

First, set up your Nutanix cluster credentials and email server details:

# Define Nutanix cluster credentials and users variables
$URI = “https://NTNX-IP:9440/PrismGateway/services/rest/v2.0/snapshots”
$NTNXUser = “NTNX-USER”
$NTNXPassword = “NTNX-PASSWORD”
$SmtpServer =”SMTP-SERVER”
$emailfrom = “report-ntnx@XXXX”
$emailto = “@XXXXX”
$subject = “Nutanix Snapshot Report”

Step 2: Fetch Snapshots from Nutanix AHV

Retrieve all snapshots from the Nutanix REST API:

# Get all snapshots from Nutanix cluster
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes((“{0}:{1}” -f $NTNXUser, $NTNXPassword)))
$NTNXSnapshots = Invoke-RestMethod -Headers @{Authorization=(“Basic {0}” -f $base64AuthInfo)} -URI $URI
$AllNTNXSnapshots = $NTNXSnapshots.entities

Step 3: Format Snapshot Data into a Table

To make the report readable, structure the snapshot data:

# Create a DataTable to store snapshot details
$Results = New-Object system.Data.DataTable “AllNTNXSnapshots”
$Columns = @(
@{Name=”UUID”; Type=[string]},
@{Name=”Snapshot-Name”; Type=[string]},
@{Name=”VM-Name”; Type=[string]},
@{Name=”Creation-Time”; Type=[string]}
)
# Add columns to the DataTable
foreach ($Column in $Columns) {
$DataColumn = New-Object System.Data.DataColumn $Column.Name, $Column.Type
$Results.Columns.Add($DataColumn)
}

Step 5: Convert Data to HTML Format and send the report 

# Process each snapshot and add details to the DataTable
foreach ($Snapshot in $AllNTNXSnapshots) {
$UUID = $Snapshot.uuid
$SnapshotName = $Snapshot.snapshot_name
$VMName = $Snapshot.vm_create_spec.name
$CreationTimeStamp = ($Snapshot.created_time) / 1000
$CreationTime = (Get-Date ‘1/1/1970’).AddMilliseconds($CreationTimeStamp)
$SnapshotCreationTime = $CreationTime.ToLocalTime()
$Row = $Results.NewRow()
$Row.”UUID” = $UUID
$Row.”Snapshot-Name” = $SnapshotName
$Row.”VM-Name” = $VMName
$Row.”Creation-Time” = $SnapshotCreationTime
$Results.Rows.Add($Row)
}
# Output results to console in table format
$Results | Format-Table -AutoSize
# Select only the desired columns for the HTML report
$SelectedColumns = $Results | Select-Object UUID, “Snapshot-Name”, “VM-Name”, “Creation-Time”
# Convert the selected columns to HTML format
$HtmlReport = $SelectedColumns | ConvertTo-Html -Fragment -PreContent “<h1>Nutanix Snapshot Report</h1>”
# Convert HTML report to a single string
$HtmlReportString = $HtmlReport -join “rn”
# Define email parameters
$EmailParams = @{
From = $emailfrom
To = $emailto
Subject = $subject
Body = $HtmlReportString
BodyAsHtml = $true
SmtpServer = $SmtpServer
}
# Send the email with the HTML report
Send-MailMessage @EmailParams

The script can be downloaded from my GitHub

Once executed, you’ll receive a report directly in your email!


Enjoy!

 

Leave a Reply

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