VMware Cloud Community
esxi1979
Expert
Expert
Jump to solution

Shutdown - Take Snapshot - PowerOn

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

Tags (1)
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

2 Replies
esxi1979
Expert
Expert
Jump to solution

I wonder doing 

Wait-Task  is any better idea & how can that be used

idea is to have minimum downtime for overall activity

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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