Merge pull request #1873 from andy-mishechkin/v-andmis/Implement_to_stop_and_to_rename_failed_vm
Implement additional feature for stopping and renaming the unstable VMs in case of failure
This commit is contained in:
@@ -48,12 +48,23 @@ Import-Module $PSScriptRoot\helpers.psm1 -DisableNameChecking
|
|||||||
# Connection to a vCenter Server system
|
# Connection to a vCenter Server system
|
||||||
Connect-VCServer
|
Connect-VCServer
|
||||||
|
|
||||||
try
|
$vm = Get-VM $VMName
|
||||||
{
|
|
||||||
Get-VM $VMName | Move-VM -Datastore $TargetDataStore -ErrorAction Stop
|
if ($env:AGENT_JOBSTATUS -eq 'Failed') {
|
||||||
Write-Host "VM has been moved successfully to target datastore '$TargetDataStore'"
|
try {
|
||||||
|
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 {
|
||||||
|
Write-Host "##vso[task.LogIssue type=error;]Failed to power off and rename VM '$VMName'"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch
|
|
||||||
{
|
try {
|
||||||
|
Move-VM -Vm $vm -Datastore $TargetDataStore -ErrorAction Stop
|
||||||
|
Write-Host "VM has been moved successfully to target datastore '$TargetDataStore'"
|
||||||
|
} catch {
|
||||||
Write-Host "##vso[task.LogIssue type=error;]Failed to move VM '$VMName' to target datastore '$TargetDataStore'"
|
Write-Host "##vso[task.LogIssue type=error;]Failed to move VM '$VMName' to target datastore '$TargetDataStore'"
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user