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
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 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 = $true)]
    param (
        [parameter(Mandatory, ParameterSetName = 'Named')]
        [string[]]$Name,
        [parameter(Mandatory, ParameterSetName = 'All')]
        [switch]$All
    )

    process {
        if ($Name) {
            foreach ($n in $Name) {
                if ($PSCmdlet.ShouldProcess($Name, "Resetting the setting back to its default value")) {
                    $config = Get-PSFConfig -FullName dbops.$n
                    if ($config) { $config.ResetValue() }
                    else {
                        Stop-PSFFunction -Message "Unable to find setting $n" -EnableException $true
                    }
                }
            }
        }
        elseif ($All) {
            foreach ($config in Get-PSFConfig -Module dbops ) {
                if ($PSCmdlet.ShouldProcess($config, "Resetting the setting back to its default value")) {
                    if ($config.Initialized) {
                        $config.ResetValue()
                    }
                    else {
                        Write-PSFMessage -Level Warning -Message "Setting $($config.fullName -replace '^dbops\.','')) was not initialized and has no default value as such"
                    }
                }
            }
        }
    }
}