Private/Export-IBConfig.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
function Export-IBConfig
{
    [CmdletBinding()]
    param()

    $coldConfig = @{
        CurrentProfile = $script:CurrentProfile
        Profiles = @{}
    }
    $profiles = $coldConfig.Profiles

    # ConvertTo-Json won't serialize the SecureString passwords in the PSCredential objects,
    # so we have to do some manual conversion into Username/Password combos that Import-IBConfig
    # will then re-hydrate later.
    $script:Profiles.Keys | ForEach-Object {
        $profiles.$_ = @{
            WAPIHost = $script:Profiles.$_.WAPIHost
            WAPIVersion = $script:Profiles.$_.WAPIVersion
            Credential = $null
            SkipCertificateCheck = $script:Profiles.$_.SkipCertificateCheck
        }
        if ($null -ne $script:Profiles.$_.Credential) {

            $credSerialized = @{ Username = $script:Profiles.$_.Credential.Username }

            # ConvertFrom-SecureString is currently only supported on Windows OSes because it
            # depends on DPAPI and throws an ugly error on Linux/MacOS. So for now, we're just
            # going to base64 encode the password on non-Windows and hope for better support
            # in future versions of PowerShell Core.
            if ('PSEdition' -notin $PSVersionTable.Keys -or $PSVersionTable.PSEdition -eq 'Desktop' -or $IsWindows) {
                $credSerialized.Password = ConvertFrom-SecureString $script:Profiles.$_.Credential.Password
            } else {
                $passPlain = $script:Profiles.$_.Credential.GetNetworkCredential().Password
                $credSerialized.Password = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($passPlain))
                $credSerialized.IsBase64 = $true
            }
            $profiles.$_.Credential = $credSerialized
        }
    }

    # Make sure the config folder exists
    New-Item $script:ConfigFolder -Type Directory -ErrorAction SilentlyContinue

    # Save it to disk.
    $coldConfig | ConvertTo-Json -Depth 5 | Out-File $script:ConfigFile -Encoding utf8

}