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 = @"