functions/Update-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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
function Update-DBOConfig {
    <#
    .SYNOPSIS
    Updates configuration file inside the existing DBOps package
     
    .DESCRIPTION
    Overwrites configuration file inside the existing DBOps package with the new values provided by user
     
    .PARAMETER Path
    Path to the existing DBOpsPackage.
    Aliases: Name, FileName, Package
     
    .PARAMETER ConfigurationFile
    A path to the custom configuration json file
    Alias: ConfigFile
     
    .PARAMETER Configuration
    Hashtable containing several configuration items at once
    Alias: Config
     
    .PARAMETER ConfigName
    Name of the configuration item to update
     
    .PARAMETER Value
    Value of the parameter specified in -ConfigName
 
    .PARAMETER Variables
    Hashtable with variables that can be used inside the scripts and deployment parameters.
    Proper format of the variable tokens is #{MyVariableName}
    Can also be provided as a part of Configuration hashtable: -Configuration @{ Variables = @{ Var1 = ...; Var2 = ...}}
     
    .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
    # Update a single parameter in the configuration file of the Package.zip package
    Update-DBOConfig Package.zip -ConfigName ApplicationName -Value 'MyApp'
 
    .EXAMPLE
    # Update several configuration parameters at once using a hashtable
    Update-DBOConfig Package.zip -Configuration @{'ApplicationName' = 'MyApp'; 'Database' = 'MyDB'}
 
    .EXAMPLE
    # Update parameters based on the contents of the json file myconfig.json
    Update-DBOConfig Package.zip -ConfigurationFile 'myconfig.json'
     
    .EXAMPLE
    # Specifically update values of the Variables parameter
    Update-DBOConfig Package.zip -Variables @{ foo = 'bar' }
     
    #>

    [CmdletBinding(DefaultParameterSetName = 'Value',
        SupportsShouldProcess = $true)]
    Param (
        [Parameter(Mandatory = $true,
            ValueFromPipeline = $true,
            Position = 1)]
        [Alias('FileName', 'Name', 'Package')]
        [string[]]$Path,
        [Parameter(ParameterSetName = 'Value',
            Mandatory = $true,
            Position = 2 )]
        [ValidateSet('ApplicationName', 'SqlInstance', 'Database', 'DeploymentMethod',
            'ConnectionTimeout', 'ExecutionTimeout', 'Encrypt', 'Credential', 'Username',
            'Password', 'SchemaVersionTable', 'Silent', 'Variables'
        )]
        [string]$ConfigName,
        [Parameter(ParameterSetName = 'Value',
            Mandatory = $true,
            Position = 3 )]
        [AllowNull()][object]$Value,
        [Parameter(ParameterSetName = 'Hashtable',
            Mandatory = $true,
            Position = 2 )]
        [Alias('Config')]
        [hashtable]$Configuration,
        [Parameter(ParameterSetName = 'File',
            Mandatory = $true,
            Position = 2 )]
        [Alias('ConfigFile')]
        [string]$ConfigurationFile,
        [Parameter(ParameterSetName = 'Variables',
            Mandatory = $true,
            Position = 2 )]
        [Parameter(ParameterSetName = 'Hashtable')]
        [Parameter(ParameterSetName = 'File')]
        [AllowNull()][hashtable]$Variables
    )
    begin {

    }
    process {
        foreach ($pFile in (Get-Item $Path)) {
            if ($package = [DBOpsPackage]::new($pFile.FullName)) {
                $config = $package.Configuration
                Write-PSFMessage -Level Verbose -Message "Assigning new values to the config"
                if ($PSCmdlet.ParameterSetName -eq 'Value') {
                    $newConfig = @{ $ConfigName = $Value }
                }
                elseif ($PSCmdlet.ParameterSetName -eq 'Hashtable') {
                    $newConfig = $Configuration
                }
                elseif ($PSCmdlet.ParameterSetName -eq 'File') {
                    $newConfig = (Get-DBOConfig -Path $ConfigurationFile).AsHashtable()
                }
                #Overriding Variables
                if ($Variables) {
                    if ($PSCmdlet.ParameterSetName -ne 'Variables') { $newConfig.Remove('Variables') }
                    $newConfig += @{ Variables = $Variables}
                }

                Write-PSFMessage -Level Verbose -Message "Saving configuration in the DBOpsPackage object"
                $config.Merge($newConfig)

                if ($pscmdlet.ShouldProcess($package, "Updating the package file")) {
                    $config.Alter()
                }
            }
        }
    }
    end {

    }
}