Private/_SaveSettings.ps1

function _SaveSettings {
    [CmdletBinding()]
    param (
        [Parameter()]
        [ValidateScript({ Test-Path $_ })]
        [string]
        $Path
    )

    $FeatureSettings = [PSCustomObject]@{
        SCCM     = $HomeWindow_EnableSCCMMenuItem.IsChecked
        Exchange = $HomeWindow_EnableExchangeMenuItem.IsChecked
    }

    $SCCMSettings = [PSCustomObject]@{
        SCCMServerHostname    = if ($ADLookups_OptionsTabSCCMServerTextBox.Text) {
            $ADLookups_OptionsTabSCCMServerTextBox.Text
        }
        else {
            $ADLookups_OptionsTabOutputLabel.Content = 'No SCCM server defined. Using default.'
            'SVP109APP'
        }
        SCCMDefaultCollection = if ($ADLookups_OptionTabSCCMCollectionComboBox.Text) {
            $ADLookups_OptionTabSCCMCollectionComboBox.Text
        }
        else {
            $ADLookups_OptionsTabOutputLabel.Content = 'No SCCM collection selected. Using default.'
            'All Client Workstations'
        }
    }

    # TODO validate a trailing '\'
    $ObjectsSettings = [PSCustomObject]@{
        ObjectStorageFolder = if ($ADLookups_OptionsTabObjectCacheTextBox.Text) {
            $ADLookups_OptionsTabObjectCacheTextBox.Text
        }
        else {
            $Path
        }
    }

    $DCSettings = [PSCustomObject]@{
        DomainController = $ADLookups_OptionTabDCComboBox.Text
    }

    $Settings = [PSCustomObject]@{
        FeatureSettings = $FeatureSettings
        SCCMSettings    = $SCCMSettings
        ObjectsSettings = $ObjectsSettings
        DCSettings      = $DCSettings
    }

    try {
        $Settings | ConvertTo-Json | Out-File "$($Path)Settings.json"
    }
    catch {
        $ADLookups_OptionsTabOutputLabel.Content = 'Saving failed.'
        break
    }

    $ADLookups_OptionsTabOutputLabel.Content = 'Settings saved.'
}