internal/functions/configuration/Write-DbatoolsConfigFile.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
function Write-DbatoolsConfigFile
{
<#
    .SYNOPSIS
        Handles config export to file.
     
    .DESCRIPTION
        Handles config export to file.
     
    .PARAMETER Config
        The configuration items to export.
     
    .PARAMETER Path
        The path to export to.
        Needs to point to the specific file to export to.
        Will create the folder structure if needed.
     
    .PARAMETER Replace
        Completely replaces previous file contents.
        By default, it will integrate settings into one coherent configuration file.
     
    .EXAMPLE
        PS C:\> Write-DbatoolsConfigFile -Config $items -Path .\file.json
     
        Exports all settings stored in $items to .\file.json.
        If the file already exists, the new settings will be merged into the existing file.
#>

    [CmdletBinding()]
    Param (
        [Sqlcollaborative.Dbatools.Configuration.Config[]]
        $Config,
        
        [string]
        $Path,
        
        [switch]
        $Replace
    )
    
    begin
    {
        $parent = Split-Path -Path $Path
        if (-not (Test-Path $parent))
        {
            $null = New-Item $parent -ItemType Directory -Force
        }
        
        $data = @{ }
        if ((Test-Path $Path) -and (-not $Replace))
        {
            foreach ($item in (Get-Content -Path $Path -Encoding UTF8 | ConvertFrom-Json))
            {
                $data[$item.FullName] = $item
            }
        }
    }
    process
    {
        foreach ($item in $Config)
        {
            $datum = @{
                Version  = 1
                FullName = $item.FullName
            }
            if ($item.SimpleExport)
            {
                $datum["Data"] = $item.Value
            }
            else
            {
                $persisted = [Sqlcollaborative.Dbatools.Configuration.ConfigurationHost]::ConvertToPersistedValue($item.Value)
                $datum["Value"] = $persisted.PersistedValue
                $datum["Type"] = $persisted.PersistedType
                $datum["Style"] = "default"
            }
            
            $data[$item.FullName] = [pscustomobject]$datum
        }
    }
    end
    {
        $data.Values | ConvertTo-Json | Set-Content -Path $Path -Encoding UTF8 -ErrorAction Stop
    }
}