functions/Reset-DBODefaultSetting.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
function Reset-DBODefaultSetting {
    <#
        .SYNOPSIS
            Resets configuration entries back to default values.
 
        .DESCRIPTION
            This function creates or changes configuration values.
            These can be used to provide dynamic configuration information outside the PowerShell variable system.
 
        .PARAMETER Name
            Name of the configuration entry.
 
        .PARAMETER All
            Specify if you want to reset all configuration values to their defaults.
 
        .PARAMETER Temporary
            The setting is not persisted outside the current session.
            By default, settings will be remembered across all powershell sessions.
 
        .PARAMETER Scope
            Choose if the setting should be stored in current user's registry or will be shared between all users.
            Allowed values: CurrentUser, AllUsers.
            AllUsers will require administrative access to the computer (elevated session).
 
            Default: CurrentUser.
 
        .PARAMETER Confirm
            Prompts to confirm certain actions
 
        .PARAMETER WhatIf
            Shows what would happen if the command would execute, but does not actually perform the command
 
        .EXAMPLE
            Reset-DBODefaultSetting -Name ConnectionTimeout
 
            Reset connection timeout setting back to default value.
 
        .EXAMPLE
            Reset-DBODefaultSetting -All
 
            Reset all settings.
    #>

    [CmdletBinding(DefaultParameterSetName = "Named", SupportsShouldProcess)]
    param (
        [parameter(Mandatory, ParameterSetName = 'Named')]
        [string[]]$Name,
        [parameter(Mandatory, ParameterSetName = 'All')]
        [switch]$All,
        [switch]$Temporary,
        [ValidateSet('CurrentUser', 'AllUsers')]
        [string]$Scope = 'CurrentUser'
    )

    process {
        if ($Name) {
            $settings = @()
            foreach ($n in $Name) {
                $config = Get-PSFConfig -FullName dbops.$n
                if ($config) { $settings += $config }
                else { Stop-PSFFunction -Message "Unable to find setting $n" -EnableException $true }
            }
        }
        elseif ($All) { $settings = Get-PSFConfig -Module dbops }
        $newScope = switch ($isWindows) {
            $false {
                switch ($Scope) {
                    'CurrentUser' { 'FileUserLocal' }
                    'AllUsers' { 'FileUserShared' }
                }
            }
            default {
                switch ($Scope) {
                    'CurrentUser' { 'UserDefault' }
                    'AllUsers' { 'SystemDefault' }
                }
            }
        }
        foreach ($config in $settings) {
            $sName = $config.fullName -replace '^dbops\.', ''
            if ($PSCmdlet.ShouldProcess($config, "Resetting the setting $sName back to its default value")) {
                if ($config.Initialized) {
                    $config.ResetValue()
                }
                else {
                    Write-PSFMessage -Level Warning -Message "Setting $sName was not initialized and has no default value as such"
                }
                if (!$Temporary) {
                    if ($PSCmdlet.ShouldProcess($Name, "Unregistering $sName in the $newScope scope")) {
                        $config | Unregister-PSFConfig -Scope $newScope
                    }
                }
            }
        }
    }
}