PSAzureAppConfiguration.psm1

#Region '.\Public\Convert-KeyReference.ps1' 0
function Convert-KeyReference {
    <#
        .SYNOPSIS
            Converts Keyreferences in a string with values retrieved from a given dictionary
        .EXAMPLE
            $dict = @(
                @{
                    Key = "MyDomain"
                    Value = "Contoso"
                },
                @{
                    Key = "MyUserName"
                    Value = "Bob"
                },
                @{
                    Key = "Environment"
                    Value = "Production"
                }
            )
            $string = '$(MyDomain)\$(MyUserName)_$(Environment)'
            Convert-KeyReference -String $string -Dictionary $dict
            Expected Output: "Contoso\Bob_Production"
        .EXAMPLE
            $dict = @(
                @{
                    Key = "Server"
                    Value = "prod-001"
                },
                @{
                    Key = "Database"
                    Value = "RetailStore"
                },
                @{
                    Key = "Trusted_Connection"
                    Value = "yes"
                },
                @{
                    Key = "ConnectionString"
                    Value = 'Server=$(Server);Database=$(Database);Trusted_Connection=$(Trusted_Connection)'
                }
            )
            $string = 'ConnectionString=$(ConnectionString)'
            Convert-KeyReference -String $string -Dictionary $dict
            Expected Output: "ConnectionString=Server=prod-001;Database=RetailStore;Trusted_Connection=yes"
    #>

    [cmdletbinding()]
    param(
        [String] $String
        ,
        $Dictionary
        ,
        [String] $Prefix = '\$\('
        ,
        [String] $Suffix = '\)'
        ,
        [int] $Cycle = 0
    )
    if ($Cycle -ge 5) {
        Return
    }
    $stringOutput = $string
    $references = Get-KeyReference -String $string -Regex "$Prefix[\w.]*$Suffix"
    if (-not $references) {
        Return $stringOutput
    }
    $references | Foreach-Object {
        $refKey = $_
        $refValue = ($Dictionary | Where-Object {$_.key -eq $refkey}).Value
        if ($refValue) {
            Write-Verbose "$refkey resolved to $refValue"
            $stringOutput = $stringOutput -replace "$Prefix$refKey$Suffix",$refValue
        }
    }
    Convert-KeyReference -String $stringOutput -Dictionary $Dictionary -Cycle ($Cycle+1)
}
#EndRegion '.\Public\Convert-KeyReference.ps1' 76
#Region '.\Public\Get-AppConfigurationKeyValue.ps1' 0
function Get-AppConfigurationKeyValue {
    [cmdletbinding()]
    param(
        [string] $Key = '*'
        ,
        [parameter(Mandatory)]
        [string] $Store
        ,
        [string] $Label = '*'
        ,
        [switch] $NoResolveSecret
        ,
        [switch] $ExcludeNoLabel
        ,
        [switch] $ConvertReferences
    )
    # az appconfig kv list -h
    if (-not $NoResolveSecret) {
        $resolveKv = '--resolve-keyvault'
    }
    $keyValues = az appconfig kv list --name $Store --label \0,$Label --key $Key $resolveKv | ConvertFrom-Json
    # when multiple keys are returned, keep the one with the label
    $Output = $keyValues | Group-object -Property Key | ForEach-Object {
        $_.Group | Sort-Object -Property Label -Descending | Select-Object -First 1
    }
    foreach ($kv in $Output) {
        $value = $kv.value
        if ($ConvertReferences) {
            $value = Convert-KeyReference -String $value -Dictionary $Output
        }
        Write-Output @{ $kv.key = $value}
    }
}
#EndRegion '.\Public\Get-AppConfigurationKeyValue.ps1' 34
#Region '.\Public\Get-KeyReference.ps1' 0
function Get-KeyReference {
    <#
        .SYNOPSIS
            Parses a string and extracts the tokenized strings
            Default prefix and suffix is '$(' and ')'
            Only words chars and dots are allowed as key strings
        .EXAMPLE
            Get-KeyReference -String 'MyServer=$(MyApp.Database.Server);Database=$(MyApp.Database.Name);Trusted_Connection=Yes'
            Expected Output: @('MyApp.Database.Server','MyApp.Database.Name')
        .EXAMPLE
            $string = '$(MyDomain)\$(MyUserName)_$(Environment)'
            Get-KeyReference -String $string
            Expected Output: @('MyDomain','MyUserName','Environment')
    #>

    param(
        [string] $String
        ,
        [string] $regex = '\$\([\w.]*\)'
    )
    $references = ($string | select-string -pattern $regex -AllMatches).Matches.Value
    if ($references) {
        $references.replace('$(','').replace(')','')
    }
}
#EndRegion '.\Public\Get-KeyReference.ps1' 25