AL/Set-AppKeyValue.ps1

<#
 .Synopsis
  Updates a value in app.json
 .Description
  Updates a value for a specific key in app.json
 .Parameter SourcePath
  Path to the current project
 .Parameter KeyName
  Name of the key the value should be updated
 .Parameter KeyValue
  New Value for the key
 .Example
  Set-AppKeyValue -KeyName "publisher" -KeyValue "test"
#>

function Set-AppKeyValue {
    [CmdletBinding(SupportsShouldProcess, ConfirmImpact="low")]
    Param(
        [Parameter(Mandatory=$false)]
        [string]$SourcePath = (Get-Location),
        [Parameter(Mandatory=$true)]
        [string]$KeyName,
        [Parameter(Mandatory=$false)]
        [object]$KeyValue
    )

    if ($PSCmdlet.ShouldProcess("app.json", "Removes key from ")) {
        if (Test-Path (Join-Path $SourcePath 'app.json')) {
            $JsonContent = Get-Content (Join-Path $SourcePath 'app.json') -Raw
            $Json = ConvertFrom-Json $JsonContent

            if ($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
        }
    }
}