PowerShell script to identify virtual machines (VMs) that have been cloned from a snapshot

The script below demonstrates how you can list VMs cloned from snapshots. It checks each VM to see if it has a parent snapshot by examining its disk’s parent disk. If the parent disk is a snapshot, it implies the VM was cloned from that snapshot.

# Connect to vCenter Server
$vcServer = 'your-vcenter-server'
Connect-VIServer -Server $vcServer -Credential (Get-Credential)

# Function to check if a VM is cloned from a snapshot
function Check-VMClonedFromSnapshot {
param (
[VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl]$VM
)

$isClonedFromSnapshot = $false
$vmDisks = Get-HardDisk -VM $VM

foreach ($disk in $vmDisks) {
$diskInfo = $disk.ExtensionData.Backing
if ($diskInfo.Parent -ne $null) {
$parentDisk = Get-View -Id $diskInfo.Parent.VmSnapshot
if ($parentDisk -ne $null) {
$isClonedFromSnapshot = $true
break
}
}
}

return $isClonedFromSnapshot
}

# Get all VMs
$vms = Get-VM

# Check each VM and report if it is cloned from a snapshot
foreach ($vm in $vms) {
if (Check-VMClonedFromSnapshot -VM $vm) {
Write-Host "$($vm.Name) is cloned from a snapshot."
}
}

# Disconnect from vCenter Server
Disconnect-VIServer -Server $vcServer -Confirm:$false

Leave a comment