Function PowerOff-VM($vm, $id){
$getvm = Get-VM -Id $id
Shutdown-VMGuest -VM $getvm -Confirm:$false | Out-Null
Write-Host "$vm is stopping!" -ForegroundColor Yellow
sleep 10
do {
$vmview = Get-VM -Id $id | Get-View
$getvm = Get-VM -Id $id
$powerstate = $getvm.PowerState
$toolsstatus = $vmview.Guest.ToolsStatus
Write-Host "$vm is stopping with powerstate $powerstate and toolsStatus $toolsstatus!" -ForegroundColor Yellow
sleep 5
}until($powerstate -match "PoweredOff")
Write-Host "$vm is powered-off"
}
$getvm = Get-VM -Id $id
Shutdown-VMGuest -VM $getvm -Confirm:$false | Out-Null
Write-Host "$vm is stopping!" -ForegroundColor Yellow
sleep 10
do {
$vmview = Get-VM -Id $id | Get-View
$getvm = Get-VM -Id $id
$powerstate = $getvm.PowerState
$toolsstatus = $vmview.Guest.ToolsStatus
Write-Host "$vm is stopping with powerstate $powerstate and toolsStatus $toolsstatus!" -ForegroundColor Yellow
sleep 5
}until($powerstate -match "PoweredOff")
Write-Host "$vm is powered-off"
}
This function is a little more straightforward. It basically calls the VMGuest to shutdown and work from there. As the previous function, it imports the same two variables ($vm and $id) for the same purposes.
If you plan on scripting for powering off a VM, don't use Stop-VM! It powers off the VM cold, and it will give an error next time you boot. Only use it if you don't care about the next boot (e.g. VM is in IndependentNonPersistent).
No comments:
Post a Comment