functions/Set-DbaConfig.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
103
104
105
106
107
108
109
110
function Set-DbaConfig
{
<#
  .SYNOPSIS
   Sets configuration entries.
   
  .DESCRIPTION
   This function creates or changes configuration values.
   These are used in a larger framework to provide dynamic configuration information outside the PowerShell variable system.
   
  .PARAMETER Name
   Name of the configuration entry. If an entry of exactly this non-casesensitive name already exists, its value will be overwritten.
   Duplicate names across different modules are possible and will be treated separately.
   If a name contains namespace notation and no module is set, the first namespace element will be used as module instead of name. Example:
   -Name "Nordwind.Server"
   Is Equivalent to
   -Name "Server" -Module "Nordwind"
   
  .PARAMETER Value
   The value to assign to the named configuration element.
   
  .PARAMETER Module
   This allows grouping configuration elements into groups based on the module/component they server.
   If this parameter is not set, the configuration element is stored under its name only, which increases the likelyhood of name conflicts in large environments.
   
  .PARAMETER Hidden
   Setting this parameter hides the configuration from casual discovery. Configurations with this set will only be returned by Get-Config, if the parameter "-Force" is used.
   This should be set for all system settings a user should have no business changing (e.g. for Infrastructure related settings such as mail server).
   
  .PARAMETER Default
   Setting this parameter causes the system to treat this configuration as a default setting. If the configuration already exists, no changes will be performed.
   Useful in scenarios where for some reason it is not practical to automatically set defaults before loading userprofiles.
   
  .EXAMPLE
   PS C:\> Set-DbaConfig -Name 'User' -Value "Friedrich"
  
   Creates a configuration entry named "User" with the value "Friedrich"
  
  .EXAMPLE
   PS C:\> Set-DbaConfig 'ConfigLink' 'https://www.example.com/config.xml' 'Company' -Hidden
  
   Creates a configuration entry named "ConfigLink" in the "Company" module with the value 'https://www.example.com/config.xml'.
   This entry is hidden from casual discovery using Get-Config.
  
  .EXAMPLE
   PS C:\> Set-DbaConfig 'Network.Firewall' '10.0.0.2' -Default
  
   Creates a configuration entry named "Firewall" in the "Network" module with the value '10.0.0.2'
   This is only set, if the setting does not exist yet. If it does, this command will apply no changes.
   
  .NOTES
   Author: Friedrich Weinmann
            Tags: Config
 #>

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true, Position = 0)]
        [string]
        $Name,
        
        [Parameter(Mandatory = $true, Position = 1)]
        [AllowNull()]
        [AllowEmptyCollection()]
        [AllowEmptyString()]
        $Value,
        
        [Parameter(Position = 2)]
        [string]
        $Module,
        
        [switch]
        $Hidden,
        
        [switch]
        $Default
    )
    
    #region Prepare Names
    $Name = $Name.ToLower()
    if ($Module) { $Module = $Module.ToLower() }
    
    if (-not $PSBoundParameters.ContainsKey("Module") -and ($Name -match ".+\..+"))
    {
        $r = $Name | select-string "^(.+?)\..+" -AllMatches
        $Module = $r.Matches[0].Groups[1].Value
        $Name = $Name.Substring($Module.Length + 1)
    }
    
    If ($Module) { $FullName = $Module, $Name -join "." }
    else { $FullName = $Name }
    #endregion Prepare Names
    
    #region Process Record
    if (([sqlcollective.dbatools.Configuration.Config]::Cfg[$FullName]) -and (-not $Default))
    {
        if ($PSBoundParameters.ContainsKey("Hidden")) { [sqlcollective.dbatools.Configuration.Config]::Cfg[$FullName].Hidden = $Hidden }
        [sqlcollective.dbatools.Configuration.Config]::Cfg[$FullName].Value = $Value
    }
    elseif (-not [sqlcollective.dbatools.Configuration.Config]::Cfg[$FullName])
    {
        $Config = New-Object sqlcollective.dbatools.Configuration.Config
        $Config.Name = $name
        $Config.Module = $Module
        $Config.Value = $Value
        $Config.Hidden = $Hidden
        [sqlcollective.dbatools.Configuration.Config]::Cfg[$FullName] = $Config
    }
    #endregion Process Record
}