Private/Test-SCVirtualMachineLiveMigrationEligibility.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
function Test-SCVirtualMachineLiveMigrationEligibility {
    #Requires -Version 3.0
    #Requires -Modules virtualmachinemanager

    [CmdletBinding()]
    [OutputType([System.Collections.Hashtable])]
    Param (
        [Parameter(Mandatory)]
        [Microsoft.SystemCenter.VirtualMachineManager.VM]$VM,
        [Parameter(Mandatory)]
        [Microsoft.SystemCenter.VirtualMachineManager.Host]$VMHost
    )

    $ErrorActionPreference = 'Stop'

    Write-Debug -Message ('ENTER {0}' -f $MyInvocation.MyCommand.Name)

    try {
        Write-Debug -Message ('ENTER TRY {0}' -f $MyInvocation.MyCommand.Name)

        Write-Debug -Message ('$VM: ''{0}''' -f $VM.Name)
        Write-Debug -Message ('$VMHost: ''{0}''' -f [string]$VMHost)

        $SCVMStatesPoweredDown = @(
            [Microsoft.VirtualManager.Utils.VMComputerSystemState]::Paused
            [Microsoft.VirtualManager.Utils.VMComputerSystemState]::PowerOff
            [Microsoft.VirtualManager.Utils.VMComputerSystemState]::Saved
        )
        Write-Debug -Message ('$SCVMStatesPoweredDown: {0}' -f [string]$SCVMStatesPoweredDown)

        $SCVMStatesRunning = @(
            [Microsoft.VirtualManager.Utils.VMComputerSystemState]::Running
        )
        Write-Debug -Message ('$SCVMStatesRunning: {0}' -f [string]$SCVMStatesRunning)

        $SCVMStatesMigrating = @(
            [Microsoft.VirtualManager.Utils.VMComputerSystemState]::UnderMigration
        )
        Write-Debug -Message ('$SCVMStatesMigrating: {0}' -f [string]$SCVMStatesMigrating)

        Write-Debug -Message '$Result = $false'
        $Result = $false
        Write-Debug -Message ('$Result: ''{0}''' -f [string]$Result)
        Write-Debug -Message '$Reason = ''OK'''
        $Reason = 'OK'
        Write-Debug -Message ('$Reason = ''{0}''' -f $Reason)

        Write-Debug -Message '$SourceVMHost = $VM.VMHost'
        $SourceVMHost = $VM.VMHost
        Write-Debug -Message ('$SourceVMHost: ''{0}''' -f [string]$SourceVMHost)
        Write-Debug -Message '$DestinationVMHost = $VMHost'
        $DestinationVMHost = $VMHost
        Write-Debug -Message ('$DestinationVMHost: ''{0}''' -f [string]$DestinationVMHost)

        Write-Debug -Message 'Read-SCVMHosts -VMHost ($SourceVMHost, $DestinationVMHost)'
        Read-SCVMHosts -VMHost ($SourceVMHost, $DestinationVMHost)

        $SourceVMs = Get-SCVirtualMachine -VMHost $SourceVMHost
        Write-Debug -Message ('$SourceVMs: ''{0}''' -f [string]$SourceVMs)
        $DestinationVMs = Get-SCVirtualMachine -VMHost $DestinationVMHost
        Write-Debug -Message ('$DestinationVMs: ''{0}''' -f [string]$DestinationVMs)

        Write-Debug -Message ('$VM.Status: ''{0}''' -f $VM.Status)
        Write-Debug -Message 'if ($VM -notin $SourceVMs)'
        if ($VM -notin $SourceVMs) {
            Write-Debug -Message '$Reason = ''NotFound'''
            $Reason = 'NotFound'
        }
        elseif ($VM -in $DestinationVMs) {
            Write-Debug -Message '$Reason = ''Migrated'''
            $Reason = 'Migrated'
        }
        elseif ($VM.Status -in $SCVMStatesMigrating) {
            Write-Debug -Message '$Reason = ''Migrating'''
            $Reason = 'Migrating'
        }
        elseif ($VM.Status -like '*Failed') {
            Write-Debug -Message '$Reason = ''Failed'''
            $Reason = 'Failed'
        }
        elseif ($VM.Status -notin $SCVMStatesRunning) {
            Write-Debug -Message '$Reason = ''NotRunning'''
            $Reason = 'NotRunning'
        }
        else {
            Write-Debug -Message '$SCVMIsBackingUp = Test-SCVirtualMachineBackingUpStatus -VM $VM'
            $SCVMIsBackingUp = Test-SCVirtualMachineBackingUpStatus -VM $VM
            Write-Debug -Message ('$SCVMIsBackingUp: ''{0}''' -f [string]$SCVMIsBackingUp)
            Write-Debug -Message 'if ($SCVMIsBackingUp)'
            if ($SCVMIsBackingUp) {
                Write-Debug -Message '$Reason = ''BackingUp'''
                $Reason = 'BackingUp'
            }
            else {
                Write-Debug -Message '$Result = $true'
                $Result = $true
            }
        }
        Write-Debug -Message ('$Reason = ''{0}''' -f $Reason)
        Write-Debug -Message ('$Result: ''{0}''' -f [string]$Result)

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

        Write-Debug -Message ('EXIT TRY {0}' -f $MyInvocation.MyCommand.Name)
    }
    catch {
        Write-Debug -Message ('ENTER CATCH {0}' -f $MyInvocation.MyCommand.Name)

        Write-Debug -Message ('{0}: $PSCmdlet.ThrowTerminatingError($_)' -f $MyInvocation.MyCommand.Name)
        $PSCmdlet.ThrowTerminatingError($_)

        Write-Debug -Message ('EXIT CATCH {0}' -f $MyInvocation.MyCommand.Name)
    }

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