We don't go past one snapshot in our environment however you are totally right. The script logic does not change though, it just needs few more loops. The script below deals with a situation where there could be 3 snapshots on a VM. If needed to consider more the script can be easily amended. Again, please move this thread to the PowerCLI section as more experienced people could look at this and comment/improve/benefit...
# Login credentials to SSH each host. It will convert the string from secure to plain text for plink
$user = Read-Host "ESXi Host SSH User"
$rootpword = Read-Host "ESXi Host SSH Password" -AsSecureString
$rootbstr = [System.Runtime.InteropServices.marshal]::SecureStringToBSTR($rootpword)
$rootpword = [System.Runtime.InteropServices.marshal]::PtrToStringAuto($rootbstr)
$vms = Get-View -ViewType virtualmachine | ?{$_.Snapshot -ne $null} | select Name
$collection = @()
foreach($vm in $vms)
{
$gather = "" | select VM,FirstSnAndType,SecondSnAndType,ThirdSnAndType,PowerState
$gather.VM = Get-VM $vm.Name
$gather.PowerState = Get-VM $vm.Name | select -ExpandProperty PowerState
$esx = Get-VM $vm.Name | Get-VMHost
# Check if the SSH service is running on the host else it will start it
Get-VMHost $esx | Get-VMHostService | Where-Object {$_.Key -eq "TSM-SSH" -and $_.Running -eq $false}|Start-VMHostService -Confirm:$false | Out-Null
# The "echo y" command automatically accepts the key for plink
echo y | C:\plink.exe -ssh $esx -l $user -pw $rootpword exit | Out-Null
# builds the vmfs path to the vmsd file
$path2 = Get-VM $vm.Name | %{$_.Extensiondata.LayoutEx.File | where {$_.Name -like "*.vmsd"}} |
select @{N="Path2";E={(($_.Name).ToString()).Split('[]')[1] }} | select -ExpandProperty Path2
$path4 = Get-VM $vm.Name | %{$_.Extensiondata.LayoutEx.File | where {$_.Name -like "*.vmsd"}} |
select @{N="Path4";E={(((($_.Name).ToString()).Split('[]')[2]).Split('/')[0]).TrimStart(" ") }} | select -ExpandProperty Path4
$vmPath = $path2+"/"+$path4
# checks whether the vmsd file has the entries which identify a VM snapshot with or without VM memory
if ((Get-VM $vm.Name | Get-Snapshot | select -ExpandProperty Name).count -ge "1" )
{
$vmsd = C:\plink.exe -ssh $esx -l $user -pw $rootpword -batch "cat /vmfs/volumes/$vmpath/*.vmsd"
$gather.VM = Get-VM $vm.Name
if($vmsd -match "snapshot0.type")
{
$gather.FirstSnAndType = "MemorySNAP - "+(Get-VM $vm.Name|Get-Snapshot|select -Index 0|select -ExpandProperty Name)
}
else
{
$gather.FirstSnAndType = "NOmemorySN - "+(Get-VM $vm.Name|Get-Snapshot|select -Index 0|select -ExpandProperty Name)
}
if($vmsd -match "snapshot1.type")
{
$gather.SecondSnAndType = "MemorySNAP - "+(Get-VM $vm.Name|Get-Snapshot|select -Index 1|select -ExpandProperty Name)
}
elseif($vmsd -match "snapshot1.uid")
{
$gather.SecondSnAndType = "NOmemorySN - "+(Get-VM $vm.Name|Get-Snapshot|select -Index 1|select -ExpandProperty Name)
}
elseif($vmsd -notmatch "snapshot1.uid")
{
$gather.SecondSnAndType = "NOSnapshot"
}
if($vmsd -match "snapshot2.type")
{
$gather.ThirdSnAndType = "MemorySNAP - "+(Get-VM $vm.Name|Get-Snapshot|select -Index 2|select -ExpandProperty Name)
}
elseif($vmsd -match "snapshot2.uid")
{
$gather.ThirdSnAndType = "NOmemorySN - "+(Get-VM $vm.Name|Get-Snapshot|select -Index 2|select -ExpandProperty Name)
}
elseif($vmsd -notmatch "snapshot2.uid")
{
$gather.ThirdSnAndType = "NOSnapshot"
}
}
$collection += $gather
}
$collection | ft