modules/ProbesMigration/ProbesMigration.psm1

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
# Load Modules
Import-Module ((Split-Path $PSScriptRoot -Parent) + "\Log\Log.psd1")
function ProbesMigration {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $True)][Microsoft.Azure.Commands.Network.Models.PSLoadBalancer] $BasicLoadBalancer,
        [Parameter(Mandatory = $True)][Microsoft.Azure.Commands.Network.Models.PSLoadBalancer] $StdLoadBalancer
    )
    log -Message "[ProbesMigration] Initiating Probes Migration"

    $probes = $BasicLoadBalancer.Probes
    foreach ($probe in $probes) {
        log -Message "[ProbesMigration] Adding Probe $($probe.Name) to Standard Load Balancer"
        $probeConfig = @{
            Name              = $probe.Name
            Port              = $probe.Port
            Protocol          = $probe.Protocol
            RequestPath       = $probe.RequestPath
            IntervalInSeconds = $probe.IntervalInSeconds
            ProbeCount        = $probe.NumberOfProbes
        }

        try {
            $ErrorActionPreference = 'Stop'
            $StdLoadBalancer | Add-AzLoadBalancerProbeConfig @probeConfig > $null
        }
        catch {
            $message = @"
            [ProbesMigration] Failed to add health probe config '$($probe.Name)' to new standard load balancer '$($stdLoadBalancer.Name)' in resource
            group '$($StdLoadBalancer.ResourceGroupName)'. To recover address the following error, and try again specifying the
            -FailedMigrationRetryFilePath parameter and Basic Load Balancer backup State file located either in this directory or
            the directory specified with -RecoveryBackupPath. `nError message: $_
"@

            log "Error" $message
            Exit
        }
    }
    log -Message "[ProbesMigration] Saving Standard Load Balancer $($StdLoadBalancer.Name)"
    try {
        $ErrorActionPreference = 'Stop'
        Set-AzLoadBalancer -LoadBalancer $StdLoadBalancer > $null
    }
    catch {
        $message = @"
        [ProbesMigration] Failed to add health probe config '$($probe.Name)' to new standard load balancer '$($stdLoadBalancer.Name)' in resource
        group '$($StdLoadBalancer.ResourceGroupName)'. To recover address the following error, and try again specifying the
        -FailedMigrationRetryFilePath parameter and Basic Load Balancer backup State file located either in this directory or
        the directory specified with -RecoveryBackupPath. `nError message: $_
"@

        log "Error" $message
        Exit
    }

    log -Message "[ProbesMigration] Probes Migration Completed"
}
Export-ModuleMember -Function ProbesMigration