Export/Public/Set-InfrastructureData.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
function Set-InfrastructureData {
    [CmdletBinding()]
    <#
    .SYNOPSIS
        ...
    .DESCRIPTION
        ...
    #>

    param(        
        [Parameter(Mandatory = $true)]
        [string]
        $ResourceGroupName,        
        [Parameter(Mandatory = $true)]
        [string]
        $ResourceLocation,        
        [Parameter(Mandatory = $true)]
        [string]
        $LoadBalancerName,
        [Parameter(Mandatory = $false)]
        [string]
        $AppScaleSetName,
        [Parameter(Mandatory = $false)]
        [string]
        $WebScaleSetName,
        [Parameter(Mandatory = $false)]
        [string]
        $StorageAccountResourceGroup = $ResourceGroupName,
        [Parameter(Mandatory = $true)]
        [string]
        $StorageAccountName,
        [Parameter(Mandatory = $true)]
        [string]
        $TableNameInfrastructure,
        [Parameter(Mandatory = $false)]
        [string]
        $TypeFilter
    )
    process {
        Write-Verbose "Updating Infrastructure table..."
        $loadBalancer = Get-AzLoadBalancer -ResourceGroupName $ResourceGroupName -Name $LoadBalancerName -ErrorAction Stop        
        $loadBalancerIpAddress = $loadBalancer.FrontendIpConfigurations.PrivateIpAddress
        $appScaleSetPrefix = ""
        if ($AppScaleSetName) {
            $vmss = Get-AzVmss -ResourceGroupName $ResourceGroupName -VMScaleSetName $AppScaleSetName -ErrorAction SilentlyContinue
            $appScaleSetPrefix = $vmss.VirtualMachineProfile.OsProfile.ComputerNamePrefix
        }
        $webScaleSetPrefix = ""
        if ($WebScaleSetName) {
            $vmss = Get-AzVmss -ResourceGroupName $ResourceGroupName -VMScaleSetName $WebScaleSetName -ErrorAction SilentlyContinue
            $webScaleSetPrefix = $vmss.VirtualMachineProfile.OsProfile.ComputerNamePrefix
        }
        $storageAccount = Get-AzStorageAccount -ResourceGroupName $StorageAccountResourceGroup -Name $StorageAccountName
        $storageAccountContext = $storageAccount.Context
        $storageAccountTable = Get-AzStorageTable -Name $TableNameInfrastructure -Context $storageAccountContext
        $cloudTable = $storageAccountTable.CloudTable
        if (-not([string]::IsNullOrEmpty($TypeFilter))) {
            $row = Get-AzTableRow -Table $cloudTable | Select-Object -First 1
        } else {
            $row = Get-AzTableRow -Table $cloudTable | Where-Object { ($_.Type -eq $TypeFilter) }
            if (-not($row)) {
                $row = Get-AzTableRow -Table $cloudTable | Select-Object -First 1
            }
        }        
        $row.ApplicationServerLoadBalancerIP = $loadBalancerIpAddress
        $row.AppServerComputerNamePrefix = $appScaleSetPrefix
        $row.WebServerComputerNamePrefix = $webScaleSetPrefix
        $row | Update-AzTableRow -table $cloudTable | Out-Null
    }    
}