Skip to content

Commit 105424b

Browse files
author
Fedor Ershov
committed
Merge branch 'dev' into 'master'
Release 1.2.0 See merge request fozzy-winadmins/SCVMReliableMigration!18
2 parents e043ae5 + 7958616 commit 105424b

7 files changed

+128
-28
lines changed

Private/Read-SCVMHosts.ps1

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,28 @@ function Read-SCVMHosts {
3939
}
4040
catch {
4141
Write-Debug -Message ('$_.Exception.HResult: {0}' -f $_.Exception.HResult)
42-
Write-Debug -Message 'if ($_.Exception.HResult -eq 2606)'
43-
if ($_.Exception.HResult -eq 2606) {
44-
Write-Debug -Message ('$Count = {0}' -f $Count)
45-
Write-Debug -Message ('$MaxAttempts = {0}' -f $MaxAttempts)
46-
Write-Debug -Message 'if ($Count -ge $MaxAttempts)'
47-
if ($Count -ge $MaxAttempts) {
48-
$Message = ('Could not refresh SCVMM Host ''{0}'' after ''{1}'' retries.' -f $SCVMHost, $MaxAttempts)
49-
$PSCmdlet.ThrowTerminatingError((New-Object -TypeName 'System.Management.Automation.ErrorRecord' -ArgumentList ((New-Object -TypeName 'System.ApplicationException' -ArgumentList ($Message, $_)), 'HostLocked', [System.Management.Automation.ErrorCategory]::InvalidResult, $null)))
50-
}
51-
Write-Debug -Message ('Start-Sleep -Seconds {0}' -f $Timeout)
52-
Start-Sleep -Seconds $Timeout
53-
}
54-
else {
42+
Write-Debug -Message ('$_.Exception.Message: {0}' -f $_.Exception.Message)
43+
Write-Debug -Message ('$_.InvocationInfo.PositionMessage: {0}' -f $_.InvocationInfo.PositionMessage)
44+
Write-Debug -Message ('$_.ScriptStackTrace: {0}' -f $_.ScriptStackTrace)
45+
Write-Debug -Message ('$_.Exception.ScriptStackTrace: {0}' -f $_.Exception.ScriptStackTrace)
46+
Write-Debug -Message ('$_.TargetObject: {0}' -f $_.TargetObject)
47+
Write-Debug -Message ('$_.FullyQualifiedErrorId: {0}' -f $_.FullyQualifiedErrorId)
48+
Write-Debug -Message ('$_.CategoryInfo.Category: {0}' -f $_.CategoryInfo.Category)
49+
Write-Debug -Message ('$_.CategoryInfo.Activity: {0}' -f $_.CategoryInfo.Activity)
50+
Write-Debug -Message ('$_.CategoryInfo.Reason: {0}' -f $_.CategoryInfo.Reason)
51+
Write-Debug -Message ('$_.CategoryInfo.TargetName: {0}' -f $_.CategoryInfo.TargetName)
52+
Write-Debug -Message ('$_.CategoryInfo.TargetType: {0}' -f $_.CategoryInfo.TargetType)
53+
54+
Write-Debug -Message ('$Count = {0}' -f $Count)
55+
Write-Debug -Message ('$MaxAttempts = {0}' -f $MaxAttempts)
56+
Write-Debug -Message 'if ($Count -ge $MaxAttempts)'
57+
if ($Count -ge $MaxAttempts) {
5558
Write-Debug -Message ('{0}: $PSCmdlet.ThrowTerminatingError($_)' -f $MyInvocation.MyCommand.Name)
5659
$PSCmdlet.ThrowTerminatingError($_)
57-
}
60+
}
61+
62+
Write-Debug -Message ('Start-Sleep -Seconds {0}' -f $Timeout)
63+
Start-Sleep -Seconds $Timeout
5864
}
5965
}
6066
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
function Repair-SCVMMigrationFailed {
2+
#Requires -Version 3.0
3+
#Requires -Modules virtualmachinemanager
4+
5+
[CmdletBinding()]
6+
Param (
7+
[Parameter(Mandatory)]
8+
[Microsoft.SystemCenter.VirtualMachineManager.Host[]]$VMHost
9+
)
10+
11+
$ErrorActionPreference = 'Stop'
12+
13+
Write-Debug -Message ('ENTER {0}' -f $MyInvocation.MyCommand.Name)
14+
15+
try {
16+
Write-Debug -Message ('ENTER TRY {0}' -f $MyInvocation.MyCommand.Name)
17+
18+
Write-Debug -Message ('$VMHost: ''{0}''' -f [string]$VMHost)
19+
20+
Write-Debug -Message 'foreach ($SCVMHost in $VMHost)'
21+
foreach ($SCVMHost in $VMHost) {
22+
Write-Debug -Message ('$SCVMHost: ''{0}''' -f [string]$SCVMHost)
23+
24+
Write-Debug -Message '$SCVMMigrationFailedList = Get-SCVirtualMachine -VMHost $SCVMHost | Where-Object -FilterScript {$_.Status -eq [Microsoft.VirtualManager.Utils.VMComputerSystemState]::MigrationFailed}'
25+
$SCVMMigrationFailedList = Get-SCVirtualMachine -VMHost $SCVMHost | Where-Object -FilterScript {$_.Status -eq [Microsoft.VirtualManager.Utils.VMComputerSystemState]::MigrationFailed}
26+
Write-Debug -Message ('$SCVMMigrationFailedList: ''{0}''' -f $SCVMMigrationFailedList.Name)
27+
28+
Write-Debug -Message 'if ($SCVMMigrationFailedList)'
29+
if ($SCVMMigrationFailedList) {
30+
31+
Write-Debug -Message 'foreach ($SCVM in $SCVMMigrationFailedList)'
32+
foreach ($SCVM in $SCVMMigrationFailedList) {
33+
Write-Debug -Message ('$SCVM: ''{0}''' -f $SCVM.Name)
34+
35+
Write-Debug -Message '$SCVM = Repair-SCVirtualMachine -VM $SCVM -Dismiss -Force'
36+
$SCVM = Repair-SCVirtualMachine -VM $SCVM -Dismiss -Force
37+
38+
Write-Debug -Message '$null = Read-SCVirtualMachine -VM $SCVM'
39+
$null = Read-SCVirtualMachine -VM $SCVM
40+
}
41+
}
42+
}
43+
Write-Debug -Message ('EXIT TRY {0}' -f $MyInvocation.MyCommand.Name)
44+
}
45+
catch {
46+
Write-Debug -Message ('ENTER CATCH {0}' -f $MyInvocation.MyCommand.Name)
47+
48+
Write-Debug -Message ('{0}: $PSCmdlet.ThrowTerminatingError($_)' -f $MyInvocation.MyCommand.Name)
49+
$PSCmdlet.ThrowTerminatingError($_)
50+
51+
Write-Debug -Message ('EXIT CATCH {0}' -f $MyInvocation.MyCommand.Name)
52+
}
53+
54+
Write-Debug -Message ('EXIT {0}' -f $MyInvocation.MyCommand.Name)
55+
56+
57+
}

Private/Test-SCVirtualMachineBackingUpStatus.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function Test-SCVirtualMachineBackingUpStatus {
2222
$VMHost = $VM.VMHost
2323
Write-Debug -Message ('$VMHost: ''{0}''' -f [string]$VMHost)
2424

25-
Write-Debug -Message ('$HVVM = Invoke-Command -ComputerName ''{0}'' -ScriptBlock {{Get-VM -Id ''{1}'')}}' -f $VMHost.Name, $VM.VMId)
25+
Write-Debug -Message ('$HVVM = Invoke-Command -ComputerName ''{0}'' -ScriptBlock {{Get-VM -Id ''{1}''}}' -f $VMHost.Name, $VM.VMId)
2626
$HVVM = Invoke-Command -ComputerName $VMHost.Name -ScriptBlock {Get-VM -Id $using:VM.VMid}
2727
Write-Debug -Message ('$HVVM: ''{0}''' -f [string]$HVVM)
2828

Private/Test-SCVirtualMachineLiveMigrationEligibility.ps1

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ function Test-SCVirtualMachineLiveMigrationEligibility {
2828
)
2929
Write-Debug -Message ('$SCVMStatesPoweredDown: {0}' -f [string]$SCVMStatesPoweredDown)
3030

31+
$SCVMStatesFailed = @(
32+
[Microsoft.VirtualManager.Utils.VMComputerSystemState]::CreationFailed
33+
[Microsoft.VirtualManager.Utils.VMComputerSystemState]::TemplateCreationFailed
34+
[Microsoft.VirtualManager.Utils.VMComputerSystemState]::CustomizationFailed
35+
[Microsoft.VirtualManager.Utils.VMComputerSystemState]::UpdateFailed
36+
[Microsoft.VirtualManager.Utils.VMComputerSystemState]::ReplacementFailed
37+
[Microsoft.VirtualManager.Utils.VMComputerSystemState]::MigrationFailed
38+
[Microsoft.VirtualManager.Utils.VMComputerSystemState]::CheckpointFailed
39+
[Microsoft.VirtualManager.Utils.VMComputerSystemState]::ShieldingFailed
40+
[Microsoft.VirtualManager.Utils.VMComputerSystemState]::P2VCreationFailed
41+
[Microsoft.VirtualManager.Utils.VMComputerSystemState]::V2VCreationFailed
42+
)
43+
Write-Debug -Message ('$SCVMStatesPoweredDown: {0}' -f [string]$SCVMStatesFailed)
44+
3145
$SCVMStatesRunning = @(
3246
[Microsoft.VirtualManager.Utils.VMComputerSystemState]::Running
3347
)
@@ -74,7 +88,7 @@ function Test-SCVirtualMachineLiveMigrationEligibility {
7488
Write-Debug -Message '$Reason = ''Migrating'''
7589
$Reason = 'Migrating'
7690
}
77-
elseif ($VM.Status -like '*Failed') {
91+
elseif ($VM.Status -in $SCVMStatesFailed) {
7892
Write-Debug -Message '$Reason = ''Failed'''
7993
$Reason = 'Failed'
8094
}
@@ -99,7 +113,7 @@ function Test-SCVirtualMachineLiveMigrationEligibility {
99113
Write-Debug -Message ('$Reason = ''{0}''' -f $Reason)
100114
Write-Debug -Message ('$Result: ''{0}''' -f [string]$Result)
101115

102-
Write-Debug -Message ('@{{Result = {0}}}; $Reason = ''{1}''; Status = ''{2}''' -f [string]$Result, $Reason, [string]$VM.Status)
116+
Write-Debug -Message ('@{{Result = {0}; $Reason = ''{1}''; Status = ''{2}''}}' -f [string]$Result, $Reason, [string]$VM.Status)
103117
@{
104118
Result = $Result
105119
Reason = $Reason

Public/Move-SCPoweredDownVirtualMachine.ps1

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ function Move-SCPoweredDownVirtualMachine {
2323
Write-Debug -Message ('$VMHost: ''{0}''' -f [string]$VMHost)
2424
Write-Debug -Message ('$Path = ''{0}''' -f $Path)
2525

26+
Write-Debug -Message '$null = Read-SCVirtualMachine -VM $VM'
27+
$null = Read-SCVirtualMachine -VM $VM
28+
2629
Write-Debug -Message '$SourceVMHost = $VM.VMHost'
2730
$SourceVMHost = $VM.VMHost
2831
Write-Debug -Message ('$SourceVMHost: ''{0}''' -f [string]$SourceVMHost)
@@ -109,20 +112,24 @@ function Move-SCPoweredDownVirtualMachine {
109112
$null = Move-SCVirtualMachine -VM $VM -VMHost $DestinationVMHost -Path $Path -JobGroup $JobGroupId
110113
}
111114
catch {
115+
Write-Debug -Message ($_)
116+
Write-Debug -Message ('Exception.HResult: {0}' -f $_.Exception.HResult)
117+
Write-Debug -Message ('$VM.Status: ''{0}''' -f [string]$VM.Status)
112118
Write-Debug -Message 'Read-SCVMHosts -VMHost ($SourceVMHost, $DestinationVMHost)'
113119
Read-SCVMHosts -VMHost ($SourceVMHost, $DestinationVMHost)
114-
Write-Debug -Message '$null = Read-SCVirtualMachine -VM $SCVM'
115-
$null = Read-SCVirtualMachine -VM $SCVM
116-
Write-Debug -Message ('$VM.VMHost: {0}' -f [string]$VM.VMHost)
117-
Write-Debug -Message ('$DestinationVMHost: {0}' -f [string]$DestinationVMHost)
120+
Write-Debug -Message 'Repair-SCVMMigrationFailed -VMHost ($SourceVMHost, $DestinationVMHost)'
121+
Repair-SCVMMigrationFailed -VMHost ($SourceVMHost, $DestinationVMHost)
122+
Write-Debug -Message '$null = Read-SCVirtualMachine -VM $VM'
123+
$null = Read-SCVirtualMachine -VM $VM
124+
Write-Debug -Message ('$VM.Status: ''{0}''' -f [string]$VM.Status)
125+
Write-Debug -Message ('$VM.VMHost: ''{0}''' -f [string]$VM.VMHost)
126+
Write-Debug -Message ('$DestinationVMHost: ''{0}''' -f [string]$DestinationVMHost)
118127
Write-Debug -Message 'if ($VM.VMHost -eq $DestinationVMHost)'
119128
if ($VM.VMHost -eq $DestinationVMHost) {
120129
Write-Debug -Message 'continue'
121130
continue
122131
}
123132
else {
124-
Write-Debug -Message ($_)
125-
Write-Debug -Message ('Exception.HResult: {0}' -f $_.Exception.HResult)
126133
Write-Debug -Message ('{0}: Throw $_' -f $MyInvocation.MyCommand.Name)
127134
throw $_
128135
}
@@ -190,4 +197,4 @@ function Move-SCPoweredDownVirtualMachine {
190197
}
191198

192199
Write-Debug -Message ('EXIT {0}' -f $MyInvocation.MyCommand.Name)
193-
}
200+
}

Public/Move-SCVirtualMachineReliably.ps1

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ function Move-SCVirtualMachineReliably {
100100
Write-Debug -Message ('$DestinationVMHost.ServerConnection.ManagedComputer.ID: ''{0}''' -f [string]$DestinationVMHost.ServerConnection.ManagedComputer.ID)
101101
Write-Debug -Message 'if ($SourceVMHost.ServerConnection.ManagedComputer.ID -eq $DestinationVMHost.ServerConnection.ManagedComputer.ID)'
102102
if ($SourceVMHost.ServerConnection.ManagedComputer.ID -eq $DestinationVMHost.ServerConnection.ManagedComputer.ID) {
103+
104+
Write-Debug -Message 'Read-SCVMHosts -VMHost ($SourceVMHost, $DestinationVMHost)'
105+
Read-SCVMHosts -VMHost ($SourceVMHost, $DestinationVMHost)
106+
107+
Write-Debug -Message 'Repair-SCVMMigrationFailed -VMHost ($SourceVMHost, $DestinationVMHost)'
108+
Repair-SCVMMigrationFailed -VMHost ($SourceVMHost, $DestinationVMHost)
109+
103110
Write-Debug -Message 'if (-not $VM)'
104111
if (-not $VM) {
105112
Write-Debug -Message '$VM = Get-SCVirtualMachine -VMHost $SourceVMHost'
@@ -167,9 +174,6 @@ function Move-SCVirtualMachineReliably {
167174
}
168175
Write-Debug -Message ('$Filter = {{{0}}}' -f $Filter)
169176

170-
Write-Debug -Message 'Read-SCVMHosts -VMHost ($SourceVMHost, $DestinationVMHost)'
171-
Read-SCVMHosts -VMHost ($SourceVMHost, $DestinationVMHost)
172-
173177
Write-Debug -Message ('$SourceSCVMs = Get-SCVirtualMachine -VMHost $SourceVMHost | Where-Object -FilterScript {{{0}}}' -f $Filter)
174178
$SourceSCVMs = Get-SCVirtualMachine -VMHost $SourceVMHost | Where-Object -FilterScript $Filter # Getting those VMs of which we care about
175179
Write-Debug -Message ('$SourceSCVMs: ''{0}''' -f [string]$SourceSCVMs.Name)
@@ -287,6 +291,10 @@ function Move-SCVirtualMachineReliably {
287291
Write-Debug -Message 'if ($CurrentLiveMigrationCount -lt $LiveMigrationMaximum)'
288292
if ($CurrentLiveMigrationCount -lt $LiveMigrationMaximum) {
289293
# If the migration queue is not full (if it is full, we do no care who filled it up)
294+
295+
Write-Debug -Message '$SCVM = Read-SCVirtualMachine -VM $SCVM'
296+
$SCVM = Read-SCVirtualMachine -VM $SCVM
297+
290298
$SCVirtualMachineLiveMigrationEligibility = Test-SCVirtualMachineLiveMigrationEligibility -VM $SCVM -VMHost $DestinationVMHost
291299
Write-Debug -Message ('$SCVirtualMachineLiveMigrationEligibility.Result: ''{0}''' -f $SCVirtualMachineLiveMigrationEligibility.Result)
292300
Write-Debug -Message ('$SCVirtualMachineLiveMigrationEligibility.Reason: ''{0}''' -f $SCVirtualMachineLiveMigrationEligibility.Reason)
@@ -350,8 +358,13 @@ function Move-SCVirtualMachineReliably {
350358
catch {
351359
Write-Debug -Message ($_)
352360
Write-Debug -Message ('Exception.HResult: {0}' -f $_.Exception.HResult)
361+
Write-Debug -Message ('$SCVM.Status: ''{0}''' -f [string]$SCVM.Status)
362+
Write-Debug -Message ('$SCVM.VMHost: ''{0}''' -f [string]$SCVM.VMHost)
363+
Write-Debug -Message 'Repair-SCVMMigrationFailed -VMHost $SCVM.VMHost'
364+
Repair-SCVMMigrationFailed -VMHost $SCVM.VMHost
353365
Write-Debug -Message '$null = Read-SCVirtualMachine -VM $SCVM'
354366
$null = Read-SCVirtualMachine -VM $SCVM
367+
Write-Debug -Message ('$SCVM.Status: ''{0}''' -f [string]$SCVM.Status)
355368
Write-Debug -Message 'Continue'
356369
Continue
357370
}
@@ -541,6 +554,9 @@ function Move-SCVirtualMachineReliably {
541554
}
542555
}
543556

557+
Write-Debug -Message 'Repair-SCVMMigrationFailed -VMHost ($SourceVMHost, $DestinationVMHost)'
558+
Repair-SCVMMigrationFailed -VMHost ($SourceVMHost, $DestinationVMHost)
559+
544560
Write-Debug -Message ('$SourceSCVMs: ''{0}''' -f [string]$SourceSCVMs.Name)
545561
Write-Debug -Message 'while ($SourceSCVMs)'
546562
}

SCVMReliableMigration.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@{
22
RootModule = 'SCVMReliableMigration.psm1'
3-
ModuleVersion = '1.1.4'
3+
ModuleVersion = '1.2.0'
44
GUID = '71e06da4-4888-4f09-9af0-f92d52cc1cda'
55
Author = 'Kirill Nikolaev'
66
CompanyName = 'Fozzy Inc.'

0 commit comments

Comments
 (0)