Files
PowerShell/vCenter Tools/GetSnaps.ps1
T
2026-07-24 17:35:40 -07:00

114 lines
3.8 KiB
PowerShell

param (
[string]$vcenter = "",
[switch]$allvms = $false,
[switch]$SaveCred = $false,
[switch]$Help = $false
)
# Variables
$fileVersion = "1.0.0"
$fileDate = "2024-12-11"
$savecred = $false
# Display Script Banner
Write-Host "--------------------------------------------------" -ForegroundColor White
Write-Host "vCenter Snapshot Report" -ForegroundColor Green
Write-Host "Version $($FileVersion) ($($FileDate))" -ForegroundColor Green
Write-Host "Written by Charlie Silverman" -ForegroundColor Green
Write-Host "--------------------------------------------------" -ForegroundColor White
if ($Help) {
Write-Host "Command Syntax:" -ForegroundColor Green
Write-Host 'GetSnaps.ps1 -vcenter="VCURI" [-help] [-savecred]' -ForegroundColor Green
Write-Host ""
Write-Host "Command line options:" -ForegroundColor Green
Write-Host "---------------------" -ForegroundColor Green
Write-Host "-vcenter - Specify the FQDN for the vCenter server" -ForegroundColor Green
Write-Host "-allvms - Get snapshots for all VMs. Omitting this option will only check powered on VMs." -ForegroundColor Green
Write-Host "-savecred - Save Credentials. If credentials were previously saved and this option is not selected," -ForegroundColor Green
Write-Host " they will be used." -ForegroundColor Green
Write-Host "-help - Show this help information." -ForegroundColor Green
exit
}
if ($vcenter -eq "") {
$vcenter = Read-Host "Enter name of vCenter Server"
}
try {
if (Test-Path -Path "$env:USERPROFILE\GetSnaps-${vcenter}.cred" -PathType Leaf) {
Write-Host "Using stored credentials for $vcenter."
$cred = Import-CliXML -path "$env:USERPROFILE\GetSnaps-${vcenter}.cred"
} else {
$cred = Get-Credential -Message "Enter credentials for vCenter $vcenter"
}
$vcenterConnection = connect-viserver $vcenter -credential $cred -ErrorAction Stop
} catch {
Write-Host "ERROR: Invalid credentials or incorrect vCenter FQDN"
Write-Host $_
exit
}
if ($savecred) {
Write-Host "Storing credentials for $vcenter."
$cred | Export-CliXML -Path "$env:USERPROFILE\GetSnaps-${vcenter}.cred"
}
$DesktopPath = [Environment]::GetFolderPath("Desktop")
$now = (Get-Date).ToString("yyyyMMdd_hhmm")
$outputFileName = "$DesktopPath\VMSnapshotList-$now.html"
# Get all VMs
if ($allvms) {
$VMs = Get-VM
} else {
$VMs = Get-VM | Where {$_.PowerState -eq "PoweredOn"}
}
# Create empty array to hold snapshot information
$SnapshotInfo = @()
# Loop through each VM and get snapshot information
foreach ($VM in $VMs) {
$Snapshots = Get-Snapshot -VM $VM
# Loop through each snapshot and add information to array
foreach ($Snapshot in $Snapshots) {
$SnapshotInfo += [PSCustomObject]@{
VMName = $VM.Name
SnapshotName = [System.Uri]::UnescapeDataString($Snapshot.Name)
Created = $Snapshot.Created
Description = $Snapshot.Description
SizeGB = [math]::Round(($Snapshot.SizeGB),2)
}
}
}
# Create HTML output
$HTML = @"
<html>
<head>
<title>Snapshots Report</title>
<style>
body {background-color: #D3D3D3;}
table {border: 3px solid black; border-collapse: collapse; font-size: 14px; font-family: Arial, sans-serif; background-color: #3B3B3B; color: white; max-width: 800px;}
th, td {border: 2px solid black; padding: 10px; text-align: left; width: auto;}
th {background-color: #2F4F4F; font-weight: bold;}
</style>
</head>
<body>
"@
$HTML += $SnapshotInfo | ConvertTo-Html -Fragment -Property VMName, SnapshotName, Created, Description, SizeGB
$HTML += @"
</body>
</html>
"@
# Output HTML to file
$HTML | Out-File -FilePath $outputFileName
# Output confirmation message
Write-Host "Generating Snapshots Report"
Write-Host "Your snapshots report has been saved to: $outputFileName"
# Disconnect from vCenter Server
Disconnect-VIServer -Confirm:$false