functions/Get-DbaConfigValue.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 Get-DbaConfigValue
{
<#
  .SYNOPSIS
   Returns the configuration value stored under the specified name.
   
  .DESCRIPTION
   Returns the configuration value stored under the specified name.
   It requires the full name (<Module>.<Name>) and is usually only called by functions.
   
  .PARAMETER Name
   The full name (<Module>.<Name>) of the configured value to return.
  
  .PARAMETER Fallback
   A fallback value to use, if no value was registered to a specific configuration element.
   This basically is a default value that only applies on a "per call" basis, rather than a system-wide default.
   
  .PARAMETER NotNull
   By default, this function returns null if one tries to retrieve the value from either a Configuration that does not exist or a Configuration whose value was set to null.
   However, sometimes it may be important that some value was returned.
   By specifying this parameter, the function will throw an error if no value was found at all.
   
  .EXAMPLE
   PS C:\> Get-DbaConfigValue -Name 'System.MailServer'
  
   Returns the configured value that was assigned to the key 'System.MailServer'
  
  .EXAMPLE
   PS C:\> Get-DbaConfigValue -Name 'Default.CoffeeMilk' -Fallback 0
  
   Returns the configured value for 'Default.CoffeeMilk'. If no such value is configured, it returns '0' instead.
   
  .NOTES
   Author: Friedrich Weinmann
            Tags: Config
 #>

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSPossibleIncorrectComparisonWithNull", "")]
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true)]
        [string]
        $Name,
        
        [object]
        $Fallback,
        
        [switch]
        $NotNull
    )
    
    $Name = $Name.ToLower()
    
    $temp = $null
    $temp = [sqlcollective.dbatools.Configuration.Config]::Cfg[$Name].Value
    if ($temp -eq $null) { $temp = $Fallback }
    
    if ($NotNull -and ($temp -eq $null))
    {
        Stop-Function -Message "No Configuration Value available for $Name" -Silent $false -Category InvalidData -Target $Name
    }
    else
    {
        return $temp
    }
}