Highlighted
Expert
Expert

Shutdown - Take Snapshot - PowerOn

Jump to solution

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)
0 Kudos
1 Solution

Accepted Solutions
Highlighted
User Moderator
User Moderator

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
Highlighted
Expert
Expert

I wonder doing 

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

idea is to have minimum downtime for overall activity

0 Kudos
Highlighted
User Moderator
User Moderator

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