-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNutanix-Snapshot-Report.ps1
More file actions
59 lines (59 loc) · 2.43 KB
/
Copy pathNutanix-Snapshot-Report.ps1
File metadata and controls
59 lines (59 loc) · 2.43 KB
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
# 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"
# 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
# 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)
}
# 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 "`r`n"
# 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