functions/Set-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
96
97
98
99
100
101
102
function Set-DBODefaultSetting {
    <#
        .SYNOPSIS
            Sets configuration entries.
 
        .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 Value
            The value to assign to the named configuration element.
 
        .PARAMETER Append
            Adds the value to the existing configuration instead of overwriting it
 
        .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
            Set-DBODefaultSetting -Name ConnectionTimeout -Value 5 -Temporary
 
            Change connection timeout setting for the current Powershell session to 5 seconds.
 
        .EXAMPLE
            Set-DBODefaultSetting -Name SchemaVersionTable -Value $null
 
            Change the default SchemaVersionTable setting to null, disabling the deployment logging by default
    #>

    [CmdletBinding(DefaultParameterSetName = "FullName", SupportsShouldProcess)]
    param (
        [parameter(Mandatory)]
        [string]$Name,
        [parameter(Mandatory)]
        [AllowNull()]
        [AllowEmptyCollection()]
        [AllowEmptyString()]
        $Value,
        [switch]$Append,
        [switch]$Temporary,
        [ValidateSet('CurrentUser', 'AllUsers')]
        [string]$Scope = 'CurrentUser'
    )

    process {
        if (-not (Get-DBODefaultSetting -Name $Name)) {
            Stop-PSFFunction -Message "Setting named $Name does not exist." -EnableException $true
            return
        }

        $newValue = $Value
        if ($append) {
            $newValue += (Get-DBODefaultSetting -Name $Name -Value)
        }
        if ($PSCmdlet.ShouldProcess($Name, 'Setting new value in the local session')) {
            Set-PSFConfig -Module dbops -Name $Name -Value $newValue -EnableException
        }

        $newScope = switch ($isWindows) {
            $false {
                switch ($Scope) {
                    'CurrentUser' { 'FileUserLocal' }
                    'AllUsers' { 'FileUserShared' }
                }
            }
            default {
                switch ($Scope) {
                    'CurrentUser' { 'UserDefault' }
                    'AllUsers' { 'SystemDefault' }
                }
            }
        }
        if (!$Temporary) {
            if ($PSCmdlet.ShouldProcess($Name, "Registering the value in the $newScope scope")) {
                try {
                    Register-PSFConfig -FullName dbops.$Name -EnableException -WarningAction SilentlyContinue -Scope $newScope
                }
                catch {
                    Set-PSFConfig -Module dbops -Name $Name -Value ($Value -join ", ") -EnableException
                    Register-PSFConfig -FullName dbops.$Name -Scope $newScope -EnableException
                }
            }
        }
        Get-DBODefaultSetting -Name $Name
    }
}