if($Vm.PowerState -ne "PoweredOff") condition has been added

This commit is contained in:
Andrey Mishechkin (GITHUB INC)
2020-10-21 21:06:52 +04:00
parent 2a693f9e18
commit 81ad74e827

View File

@@ -52,7 +52,9 @@ $Vm = Get-VM $VMName
if($env:AGENT_JOBSTATUS -eq 'Failed') {
try {
Stop-Vm -Vm $Vm -Confirm:$false -ErrorAction Stop
if($Vm.PowerState -ne "PoweredOff") {
Stop-Vm -Vm $Vm -Confirm:$false -ErrorAction Stop
}
Set-Vm -Vm $Vm -Name "${VMName}_failed" -Confirm:$false -ErrorAction Stop
Write-Host "VM has been successfully powered off and renamed to [${VMName}_failed]"
} catch {