Functions/Set-SettingsValue.ps1

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

    param(
        [PSCustomObject] $settings,
        [string] $name,
        [object] $value
    )
    $isJson = Test-JsonCustom $value
    Write-Debug "Setting: $name from parameters - isJson: $isJSon"
    
    if ($isJson) {
        $value = $value | ConvertFrom-Json
    }
    $dotpos = $name.indexof(".")
    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
        }
        else {
            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
            }
            else {
                $settings.$name = $value
            }
        }
        else {
            Write-Verbose "Adding $name -Value $value "
            $settings | Add-Member -MemberType NoteProperty -Name  $name -Value $value -Force
        }
    }

}