AL/Set-AppKeyValue.ps1

function Set-AppKeyValue {
    Param(
        [Parameter(Mandatory=$false)]
        [string]$SourcePath = (Get-Location),
        [Parameter(Mandatory=$true)]
        [string]$KeyName,
        [Parameter(Mandatory=$false)]
        $KeyValue
    )

    if (Test-Path (Join-Path $SourcePath 'app.json')) {
        $JsonContent = Get-Content (Join-Path $SourcePath 'app.json') -Raw
        $Json = ConvertFrom-Json $JsonContent

        if (($Null -eq $KeyValue) -or ($KeyValue -eq '')) {
            $Json.PSObject.Properties.Remove($KeyName)
        }
        else {
            if ($null -eq $Json.PSObject.Properties.Item($KeyName)) {
                $Json.PSObject.Properties.Add((New-Object System.Management.Automation.PSNoteProperty($KeyName,$KeyValue)))
            }
            else {
                $Json.PSObject.Properties.Item($KeyName).Value = $KeyValue
            }
        }
        
        $JsonContent = ConvertTo-Json $Json
    
        Set-Content -Path (Join-Path $SourcePath 'app.json') -Value $JsonContent
    }
    else {
        $JsonContent = '"{0}": "{1}"' -f $KeyName, $KeyValue
        $JsonContent = '{' + [Environment]::NewLine + $JsonContent + [Environment]::NewLine + '}'
        Add-Content (Join-Path $SourcePath 'app.json') -Value $JsonContent
    }
}


Export-ModuleMember -Function Set-AppKeyValue