LucD you provided in 2011 below
$VM = (Get-Content c:\temp\servers.txt)
foreach($vm in Get-VM -Name $VM){
Shutdown-VMguest -VM $vm -confirm:$false
while ($vm.ExtensionData.Runtime.PowerState -eq "poweredOn"){
Start-Sleep -Seconds 2
$vm.ExtensionData.UpdateViewData("Runtime.PowerState")
}
New-Snapshot -VM $vm -name (get-date)
Start-VM -VM $vm
}
I think above still work, but we are in 2020 , did the powecli team added any better way to handle this or this is it now as well.
Thanks
Wait-Task is indeed an alternative, but it has its flaws.
See for example errors reported in wait-task
By using the API properties as in that code snippet you included, you have more freedom to decide what happens when an error occurs.
Remember that a cmdlet, under the covers, uses the same concept (API methods and properties), but it is a specific implementation (designed by the PowerCLI Dev Team).
Some features, like intercepting errors, might not be implemented in a way as you would need them to be.
My point of view, use the cmdlet if it handles all the possibilities you foresee for your script.
For finer tuning (and coding) go for the API methods and properties.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I wonder doing
idea is to have minimum downtime for overall activity
Wait-Task is indeed an alternative, but it has its flaws.
See for example errors reported in wait-task
By using the API properties as in that code snippet you included, you have more freedom to decide what happens when an error occurs.
Remember that a cmdlet, under the covers, uses the same concept (API methods and properties), but it is a specific implementation (designed by the PowerCLI Dev Team).
Some features, like intercepting errors, might not be implemented in a way as you would need them to be.
My point of view, use the cmdlet if it handles all the possibilities you foresee for your script.
For finer tuning (and coding) go for the API methods and properties.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference