I did some polishing of your script and came to this:
Connect-VIServer -Server $server
$dsnum = 2
Get-Cluster "cluster1" | Get-VM | ForEach-Object {
$vm = $_
$Datastores = $vm | Get-Datastore
$Count = ($Datastores | Measure-Object).Count
if ($Count -ge $dsnum )
{
Write-host "Listed DataStores $Datastores" -BackgroundColor DarkMagenta -ForegroundColor White
Write-Host "Match $($vm.Name)" -fore Blue
$vm | New-Snapshot -Name "Snap01"
$vm | Get-Snapshot | Remove-Snapshot -confirm:$false
$NewDatastores = $vm | Get-Datastore
write-host "Updated $($vm.Name) Listed DataStores $NewDatastores" -ForegroundColor Cyan
}
else {
write-host "Server Not Matched $($vm.Name)" -BackgroundColor Black -ForegroundColor White
}
}
Disconnect-VIServer -confirm:$false
I didn't test the script. But I would like to hear from you if it works oke.
Regards, Robert