functions/Get-DBOConfig.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
function Get-DBOConfig {
    <#
    .SYNOPSIS
    Returns a DBOpsConfig object
     
    .DESCRIPTION
    Returns a DBOpsConfig object from an existing json file. If file was not specified, returns a blank DBOpsConfig object.
    Values of the config can be overwritten by the hashtable parameter -Configuration.
     
    .PARAMETER Path
    Path to the JSON config file.
         
    .PARAMETER Configuration
    Overrides for the configuration values. Will replace existing configuration values.
 
    .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
    # Returns empty configuration
    Get-DBOConfig
     
    .EXAMPLE
    # Returns configuration from existing file
    Get-DBOConfig c:\package\dbops.config.json
 
    .EXAMPLE
    # Saves empty configuration to a file
    (Get-DBOConfig).SaveToFile('c:\package\dbops.config.json')
 
    #>

    [CmdletBinding()]
    param
    (
        [string]$Path,
        [object]$Configuration
    )
    if ($Path) {
        Write-PSFMessage -Level Verbose -Message "Reading configuration from $Path"
        $config = [DBOpsConfig]::FromFile($Path)
    }
    else {
        Write-PSFMessage -Level Verbose -Message "Generating blank configuration object"
        $config = [DBOpsConfig]::new()
    }
    if ($Configuration) {
        if ($Configuration -is [DBOpsConfig] -or $Configuration -is [hashtable]) {
            Write-PSFMessage -Level Verbose -Message "Merging configuration"
            $config.Merge($Configuration)
        }
        else {
            Stop-PSFFunction -EnableException $true -Message "The following object type is not supported: $($InputObject.GetType().Name). The only supported types are DBOpsConfig and Hashtable."
        }
    }
    $config
}