internal/configurations/configuration.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
<#
 
#-------------------------#
# Warning Warning Warning #
#-------------------------#
 
This is the global configuration management file.
 
DO NOT EDIT THIS FILE!!!!!
Disobedience shall be answered by the wrath of Fred.
You've been warned.
;)
 
The purpose of this file is to manage the configuration system.
That means, messing with this may mess with every function using this infrastructure.
Don't, unless you know what you do.
 
#---------------------------------------#
# Implementing the configuration system #
#---------------------------------------#
 
The configuration system is designed, to keep as much hard coded configuration out of the functions.
Instead we keep it in a central location: The Configuration store (= this folder).
 
In Order to put something here, either find a configuration file whose topic suits you and add configuration there,
or create your own file. The configuration system is loaded last during module import process, so you have access to all
that dbatools has to offer.
 
Examples are better than a thousand words:
 
a) Setting the configuration value
# Put this in a configuration file in this folder
Set-DbaConfig -Name 'Path.DbatoolsLog' -Value "$($env:AppData)\PowerShell\dbatools\dbatools.log" -Default
 
b) Retrieving the configuration value in your function
# Put this in the function that uses this setting
$path = Get-DbaConfigValue -Name 'Path.DbatoolsLog' -FallBack ("$($env:temp)\dbatools.log")
 
# Explanation #
#-------------#
 
In step a), which is run during module import, we assign the configuration of the name 'Path.DbatoolsLog' the value "$($env:AppData)\PowerShell\dbatools\dbatools.log"
Unless there already IS a value set to this name (that's what the '-Default' switch is doing).
That means, that if a user had a different configuration value in his profile, that value will win. Userchoice over preset.
ALL configurations defined by the module should be 'default' values.
 
In step b), which will be run whenever the function is called within which it is written, we retrieve the value stored behind the name 'Path.DbatoolsLog'.
If there is nothing there (for example, if the user accidentally removed or nulled the configuration), then it will fall back to using "$($env:temp)\dbatools.log"
 
#>


#region Implement user profile
if ($var = Get-Variable "dbatools_config" -Scope Global -ErrorAction Ignore)
{
    if ($var.Value.GetType().FullName -eq "System.Management.Automation.ScriptBlock")
    {
        $var.Value.Invoke()
    }
}
#endregion Implement user profile

# Import other configuration files
foreach ($file in (Get-ChildItem -Path $PSScriptRoot -Exclude "configuration.ps1"))
{
    . $file.FullName
}