AL/Set-EnvironmentKeyValue.ps1

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

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

        $Json.PSObject.Properties.Item($KeyName).Value = $KeyValue
        
        $JsonContent = ConvertTo-Json $Json
    
        Set-Content -Path (Join-Path $SourcePath 'environment.json') -Value $JsonContent
    }
    else {
        $JsonContent = '"{0}": "{1}"' -f $KeyName, $KeyValue
        $JsonContent = '{' + [Environment]::NewLine + $JsonContent + [Environment]::NewLine + '}'
        Add-Content (Join-Path $SourcePath 'environment.json') -Value $JsonContent
    }
}


Export-ModuleMember -Function Set-EnvironmentKeyValue