Quick blog post about using a csv to check VMs with snapshots and their details
# Read the VM names from a text file (one VM name per line)
$VMNames = Get-Content -Path "C:\path\to\your\VMNames.txt"
# Initialize an empty list to hold the results
$Results = @()
# Create a set to track processed VMs by name
$ProcessedVMs = @()
# Loop through each VM name
foreach ($vmName in $VMNames) {
# Trim any extra spaces from the VM name
$vmName = $vmName.Trim()
# Debugging output: display the VM name being processed
Write-Host "Processing VM: $vmName"
# Check if the VM has already been processed to avoid duplicates
if ($ProcessedVMs -contains $vmName) {
Write-Host "VM $vmName already processed, skipping."
continue
}
# Get the VM object using the clean VM name
$VMObjects = Get-VM -Name $vmName -ErrorAction SilentlyContinue
# If more than one VM object is returned (e.g., template or placeholder), filter and pick the correct one
if ($VMObjects.Count -gt 1) {
# Filter out SRM placeholders and templates, and get a valid powered-on or powered-off VM
$VMObject = $VMObjects | Where-Object { $_.ExtensionData.Config.Template -eq $false -and ($_.PowerState -eq "PoweredOn" -or $_.PowerState -eq "PoweredOff") } | Select-Object -First 1
} else {
$VMObject = $VMObjects
}
# Check if the VM exists and is valid
if ($VMObject) {
# Mark this VM as processed to avoid future duplicates
$ProcessedVMs += $vmName
# Debugging output to show which VM is selected
Write-Host "Adding snapshot for VM: $($VMObject.Name)"
# Get snapshots for the VM (if it has any)
$Snapshots = Get-Snapshot -VM $VMObject -ErrorAction SilentlyContinue
# Get additional VM information
$VMHost = $VMObject.VMHost.Name
$Cluster = (Get-Cluster -VMHost $VMHost).Name
$VCenter = (Get-View -Id "ServiceInstance").Content.About.InstanceName
foreach ($Snapshot in $Snapshots) {
# Check if the snapshot information is already in the results to avoid duplicates
$existingSnapshot = $Results | Where-Object { $_.VMName -eq $VMObject.Name -and $_.SnapshotName -eq $Snapshot.Name }
# If not already added, add it to the results
if (-not $existingSnapshot) {
# Add the snapshot info to the list
$SnapshotInfo = New-Object PSObject -property @{
VMName = $VMObject.Name # Ensure only the VM name is included
SnapshotName = $Snapshot.Name
Description = $Snapshot.Description
Created = $Snapshot.Created
SizeGB = [math]::round($Snapshot.SizeGB, 2) # Round to 2 decimal places
Host = $VMHost
Cluster = $Cluster
vCenter = $VCenter
}
# Add the result to the list
$Results += $SnapshotInfo
}
}
} else {
Write-Host "VM $vmName not found in vCenter."
}
}
# Export results if found
if ($Results.Count -gt 0) {
$CurrentDate = Get-Date -Format "MM-dd-yyyy"
$ExportPath = "C:\path\to\SnapshotDetails_$CurrentDate.csv"
$Results | Select-Object VMName, SnapshotName, Description, Created, SizeGB, Host, Cluster, vCenter | Export-Csv -Path $ExportPath -NoTypeInformation
Write-Host "Snapshot details exported to $ExportPath"
} else {
Write-Host "No snapshots found for the specified VMs."
}
##AddHostvCenter