Functions/Set-SettingsValue.ps1

Function Set-SettingsValue {
    [CmdletBinding()]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '', Justification = 'Don''t nee ShouldPorcess')]

    param(
        [PSCustomObject] $settings,
        [string] $name,
        [object] $value,
        [string] $SettingDelimiter ,
        [switch] $ThrowIfNotFound
    )
    $isJson = Test-JsonCustom $value
    Write-Debug "Setting: $name from parameters - isJson: $isJSon"
    
    if ($isJson) {
        $value = $value | ConvertFrom-Json
    }
    $dotpos = $name.indexof($SettingDelimiter )
    if ($dotpos -gt 0) {
        $namePart1 = $name.substring(0,$dotpos)
        $nameRemainder = $name.substring($dotpos+1)
        if ($null -ne $settings.$namePart1) {
            Set-SettingsValue -Settings $settings.$namePart1 -name $nameRemainder -value $value -SettingDelimiter $SettingDelimiter -ThrowIfNotFound:$ThrowIfNotFound
        }
        elseif ($ThrowIfNotFound) {
            Throw "Setting doesn't exist for $namePart1" 
        }
    }
    else {
        if ($null -ne $settings.$name) {
            # Write-Verbose "Overriding $($property.Name) -Value $($property.Value) type $($currentSettings.$Prop.GetType().Name)"
            if ($settings.$name -is [PSObject]) {
                Write-Verbose "Property $($name) is Object so merging that with property of type $($value.GetType())"    
                Merge-Settings -CurrentSettings $settings.$name -newSettings $value -ignoreMissing
            }
            else {
                $settings.$name = $value
            }
        }
        else {
            Write-Verbose "ignoreing as setting doesn't exist $name -Value $value "
    # $settings | Add-Member -MemberType NoteProperty -Name $name -Value $value -Force
        }
    }

}