Public/ps1/Configuration/General/Set-ApprxrConfigurationValue.ps1

<#
    .SYNOPSIS
    Sets a configuration value in the Apprxr config file.
 
    .DESCRIPTION
    Writes or updates the specified property in config.json. Supports storing secure strings if requested.
 
    .PARAMETER name
    The name of the configuration property to set.
 
    .PARAMETER value
    The value to store for the configuration property.
 
    .PARAMETER secure
    If specified, stores the value as a secure string.
 
    .EXAMPLE
    Set-ApprxrConfigurationValue -name 'UserName' -value 'admin'
 
    .NOTES
    Used for updating configuration values in Apprxr scripts and modules.
#>

function Set-ApprxrConfigurationValue{
    param($name, $value, [switch]$secure)

    
    $folder = Get-ApprxrConfigurationFolder


    if ($secure) {
        $value = ConvertTo-SecureString -Force -AsPlainText $value | ConvertFrom-SecureString 
    }
    
    if (Test-Path "$folder\config.json") {
    } else {
        "{}"|set-content "$folder\config.json"
    }

    $config = Get-Content "$folder\config.json" | ConvertFrom-Json
    # the prpoerty does not exist
    
    if ($config.$name ) {
        $config.$name = $value
    } elseif (@($config.psobject.Properties).Count -ge 1 -and $config.PSobject.Properties.Name.Contains($name)) {
        $config.$name = $value
    }
    
    else {
        $config | Add-Member -MemberType NoteProperty -Name $name -Value $value
    }


    $config | ConvertTo-Json | Set-Content "$folder\config.json" 

}