AL/Set-EnvironmentKeyValue.ps1
<# .Synopsis Updates a value in settings.json .Description Updates a value for a specific key in settings.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-EnvironmentKeyValue -KeyName "publisher" -KeyValue "test" #> function Set-EnvironmentKeyValue { [CmdletBinding(SupportsShouldProcess, ConfirmImpact="low")] Param( [Parameter(Mandatory=$false)] [string]$SourcePath = (Get-Location), [Parameter(Mandatory=$true)] [string]$KeyName, [Parameter(Mandatory=$true)] [string]$KeyValue ) if ($PSCmdlet.ShouldProcess("settings.json", "Updates value of key in ")) { 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 } } } |