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

    try {
        $ErrorActionPreference = 'Stop'
        $vmssId = $BasicLoadBalancer.BackendAddressPools.BackendIpConfigurations.id | Foreach-Object { $_.split("virtualMachines")[0] } | Select-Object -Unique
        $vmssRg = $vmssId.Split('/')[4]
        $vmssName = $vmssId.Split('/')[8]
        log -Message "[GetVMSSFromBasicLoadBalancer] Loading VMSS $vmssName from RG $vmssRg"
        $vmss = Get-AzVmss -ResourceGroupName $vmssRg -VMScaleSetName $vmssName
    }
    catch {
        $message = @"
        [GetVMSSFromBasicLoadBalancer] An error occured when getting VMSS '$($vmssName)' in resource group '$($vmssRG)'. 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 "[GetVMSSFromBasicLoadBalancer] VMSS loaded Name $($vmss.Name) from RG $($vmss.ResourceGroupName)"
    return , $vmss
}

Export-ModuleMember -Function GetVMSSFromBasicLoadBalancer