Public/Save-IBWAPIConfig.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
function Save-IBWAPIConfig
{
    [CmdletBinding()]
    param(
        [Alias('host')]
        [string]$WAPIHost,
        [switch]$AllHosts
    )

    function CopyConfig($src,$target) {
        if ($src.WAPIHost) { $target.WAPIHost = $src.WAPIHost }
        if ($src.WAPIVersion) { $target.WAPIVersion = $src.WAPIVersion }
        if ($src.Credential) { $target.Credential = $src.Credential }
        if ($src.IgnoreCertificateValidation) { $target.IgnoreCertificateValidation = $src.IgnoreCertificateValidation }

        # We don't want to save WebSession because it doesn't serialize well and is
        # likely to be expired in the future anyway. BUT, we don't want to lose the
        # embedded Credential if an explicit one isn't already set. So pull it out and
        # save it.
        if ($src.WebSession -and !$src.Credential) {
            $netCred = $src.WebSession.Credentials
            $target.Credential = New-Object PSCredential($netCred.Username,($netCred.Password | ConvertTo-SecureString -AsPlainText -Force))
        }
    }

    # create an in-memory copy of the currently cold config data
    $coldConfig = Import-IBWAPIConfig

    if ($AllHosts) {

        # Loop through all of the existing hosts and overwrite any existing entries
        # in the cold config with a copy of the live config
        @($script:Config.Keys) | ForEach-Object {
            Write-Verbose "Saving $_"
            $coldConfig.Hosts.$_ = @{}
            CopyConfig $script:Config.$_ $coldConfig.Hosts.$_
        }

    } else {

        # decide which host to save
        $hostToSave = $script:CurrentHost
        if (![string]::IsNullOrWhiteSpace($WAPIHost)) {
            $hostToSave = $WAPIHost
        }

        # Overwrite the existing entry for the host if it exists
        if ($hostToSave -in $script:Config.Keys) {
            Write-Verbose "Saving $hostToSave"
            $coldConfig.Hosts.$hostToSave = @{}
            CopyConfig $script:Config.$hostToSave $coldConfig.Hosts.$hostToSave
        } else {
            Write-Warning "$hostToSave not found in the set of existing configs."
        }

    }

    # add the current host if it's set
    if (![string]::IsNullOrWhiteSpace($script:CurrentHost)) {
        $coldConfig.CurrentHost = $script:CurrentHost
    }

    Export-IBWAPIConfig $coldConfig




    <#
    .SYNOPSIS
        Persist WAPI configuration data to disk.
 
    .DESCRIPTION
        When calling this function with no parameters, the currently active set of config values will be persisted to the user's local profile.
 
        When called with -WAPIHost, the set of config values for that host will be persisted.
 
        When called with -AllHosts, all sets of config values will be persisted.
 
    .PARAMETER WAPIHost
        The fully qualified DNS name or IP address of the Infoblox WAPI endpoint (usually the grid master).
 
    .PARAMETER AllHosts
        If set, all sets of config values will be persisted to disk.
 
    .EXAMPLE
        Save-IBWAPIConfig
 
        Saves the currently active WAPI config set to disk.
 
    .EXAMPLE
        Save-IBWAPIConfig -AllHosts
 
        Saves all config sets to disk.
 
    .LINK
        Project: https://github.com/rmbolger/Posh-IBWAPI
 
    .LINK
        Set-IBWAPIConfig
 
    .LINK
        Remove-IBWAPIConfig
 
    #>

}