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 = '\)'
    )
    $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
        Write-Verbose "$refkey resolved to $refValue"
        $stringOutput = $stringOutput -replace "$Prefix$refKey$Suffix",$refValue
    }
    Convert-KeyReference -String $stringOutput -Dictionary $Dictionary
}
#EndRegion '.\Public\Convert-KeyReference.ps1' 69
#Region '.\Public\Get-AppConfigurationKeyValue.ps1' 0
function Get-AppConfigurationKeyValue {
    [cmdletbinding()]
    param(
        [string] $Key = '*'
        ,
        [string] $Store
        ,
        [string] $Label = '*'
        ,
        [switch] $NoResolveSecret
        ,
        [switch] $ExcludeNoLabel
    )
    # az appconfig kv list -h
    If (-not $NoResolveSecret) {
        $resolveKv = '--resolve-keyvault'
    }
    $Output = az appconfig kv list --name $Store --label \0 --key $Key $resolveKv | ConvertFrom-Json
    $Output += az appconfig kv list --name $Store --label $Label --key $Key $resolveKv | ConvertFrom-Json

    ForEach ($kv in $Output) {
        $value = $kv.value
        $value = Convert-KeyReference -String $value -Dictionary $Output
        Write-Output @{ $kv.key = $value}
    }
}
#EndRegion '.\Public\Get-AppConfigurationKeyValue.ps1' 27
#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