Export/Private/Get-EnvironmentsFromStorage.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
function Global:Get-EnvironmentsFromStorage {
    [CmdletBinding()]
    <#
    .SYNOPSIS
        ...
    .DESCRIPTION
        ...
    #>

    param(
        [Parameter(Mandatory = $true)]
        $StorageAccountContext,
        [Parameter(Mandatory = $true)]
        [string]
        $TableNameEnvironments,
        [Parameter(Mandatory = $false)]
        [string]
        $TableNameDefaults,
        [Parameter(Mandatory = $true)]
        [string]
        $TypeFilter,
        [Parameter(Mandatory = $false)]
        [ValidateSet('Application', 'Web')]
        [string]
        $ConfigType = "Application",
        [Parameter(Mandatory = $false)]
        [switch]
        $EnvironmentsOnly
    )
    process {
        Write-Verbose "Loading Environment-data from Storage-table $TableName..."
        $environments = @()
        if (-not($EnvironmentsOnly)){
            if ([string]::IsNullOrEmpty($TableNameDefaults)){
                throw "TableNameDefaults might not be empty"
            }
        }
        $storageAccountTable = Get-AzStorageTable -Name $TableNameEnvironments -Context $StorageAccountContext
        $cloudTable = $storageAccountTable.CloudTable
        $rows = Get-AzTableRow -Table $cloudTable  | Where-Object { ($_.Type -eq $TypeFilter) }
        foreach ($row in $rows) {
            $instance = [pscustomobject]@{
                ServerInstance         = $row.ServiceName;
                DatabaseServer         = $row.DatabaseServer;
                DatabaseInstance       = $row.DatabaseInstance;
                Databasename           = $row.DatabaseName;
                ClientServicesPort     = $row.ClientServicesPort ;
                ManagementServicesPort = $row.ManagementServicesPort 
                SoapServicesPort       = $row.SoapServicesPort;
                OdataServicesPort      = $row.ODataServicesPort;
                DeveloperServicesPort  = $row.DeveloperServicesPort;
                Authentication         = $row.AuthType
                KVCredentialIdentifier = $row.KVCredentialIdentifier
                Settings               = @{ }
            }
            $environments += $instance
        }
        if ($EnvironmentsOnly){
            $environments
            return
        }
        foreach ($environment in $environments) {
            $storageAccountTable = Get-AzStorageTable -Name $TableNameDefaults -Context $storageAccountContext.Context
            $cloudTable = $storageAccountTable.CloudTable
            if ($ConfigType -eq "Application"){
                $rows = Get-AzTableRow -Table $cloudTable | Where-Object { (($_.ServiceName -eq $null) -or ($_.ServiceName -eq "") -or ($_.ServiceName -eq $instanceRow.ServiceName)) -and (($_.WebConfig -eq $false) -or ($_.WebConfig -eq $null)) }
            } else {
                $rows = Get-AzTableRow -Table $cloudTable | Where-Object { (($_.ServiceName -eq $null) -or ($_.ServiceName -eq "") -or ($_.ServiceName -eq $instanceRow.ServiceName)) -and (($_.WebConfig -eq $true)) }
            }
            $settings = @{}
            foreach ($row in $rows) {
                $settings.Add($row.KeyName, $row.KeyValue)
            }
            $environment.Settings = $settings
        }
        $environments
    }
}