Private/_SaveSettings.ps1

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

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

    $SCCMSettings = [PSCustomObject]@{
        # TODO do i need these if statements? the initial settings and first run should catch this.
        SCCMServerHostname    = if ($ADLookups_OptionsTabSCCMServerTextBox.Text) {
            $ADLookups_OptionsTabSCCMServerTextBox.Text
        }
        else {
            $ADLookups_OptionsTabOutputLabel.Content = 'No SCCM server defined. Using default.'
            'SCCM'
        }
        SCCMDefaultCollection = if ($ADLookups_OptionTabSCCMCollectionComboBox.Text) {
            $ADLookups_OptionTabSCCMCollectionComboBox.Text
        }
        else {
            $ADLookups_OptionsTabOutputLabel.Content = 'No SCCM collection selected. Using default.'
            'All Systems'
        }
    }

    $ObjectsSettings = [PSCustomObject]@{
        ObjectStorageFolder = if ($ADLookups_OptionsTabObjectCacheTextBox.Text[-1] -eq '\') {
            $ADLookups_OptionsTabObjectCacheTextBox.Text
        }
        else {
            $ADLookups_OptionsTabObjectCacheTextBox.Text + '\'
        }
    }

    $DCSettings = [PSCustomObject]@{
        DomainController = if ($ADLookups_OptionTabDCComboBox.Text) {
            $ADLookups_OptionTabDCComboBox.Text
        }
        else {
            # This is defined in the pre-run checks
            $PDC.Name
        }
    }

    $Settings = [PSCustomObject]@{
        Theme           = if ($HomeWindow_DarkThemeMenuItem.IsChecked) {
            'Dark'
        }
        elseif ($HomeWindow_VioletThemeMenuItem.IsChecked) {
            'Violet'
        }
        elseif ($HomeWindow_LightThemeMenuItem.IsChecked) {
            'Light'
        }
        else {
            'Light'
        }
        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.'

    $MessageSplat = @{
        MessageText  = 'Restart ServerTeamTools to use the newly saved settings.'
        MessageIcon  = 'Asterisk'
        ButtonType   = 'OK'
        MessageTitle = 'Restart function'
    }
    _ShowMessageBox @MessageSplat
}