Public/Remove-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
107
108
109
110
111
112
113
114
115
116
117
function Remove-IBWAPIConfig
{
    [CmdletBinding()]
    param(
        [Alias('host')]
        [string]$WAPIHost,
        [switch]$AllHosts
    )

    if ($AllHosts) {

        Write-Verbose "Removing all config sets."

        # delete the config file if it exists
        if (Test-Path $script:ConfigFile) {
            Remove-Item $script:ConfigFile -Force
        }

        # load the now empty cold config
        $coldConfig = Import-IBWAPIConfig
        $script:CurrentHost = $coldConfig.CurrentHost
        $script:Config = $coldConfig.Hosts

    } else {

        # load the cold config
        $coldConfig = Import-IBWAPIConfig

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

        Write-Verbose "Removing $hostToRemove"

        # remove from memory first
        if ($hostToRemove -in $script:Config.Keys) {

            $script:Config.Remove($hostToRemove)

            # set a new CurrentHost if necessary
            if ($script:CurrentHost -eq $hostToRemove) {
                $script:CurrentHost = [string]::Empty
                if ($script:Config.Count -gt 0) {
                    $script:CurrentHost = @(($script:Config.Keys | Sort-Object))[0]
                }
            }

        } else {
            Write-Warning "$hostToRemove not found in the set of existing configs."
        }

        # now remove from disk
        if ($hostToRemove -in $coldConfig.Hosts.Keys) {

            # if this is the last entry on disk, just remove the file
            if ($coldConfig.Hosts.Count -le 1) {
                if (Test-Path $script:ConfigFile) {
                    Remove-Item $script:ConfigFile -Force
                }
            } else {
                # otherwise, remove just this entry
                $coldConfig.Hosts.Remove($hostToRemove)

                # set the same CurrentHost as in memory
                $coldConfig.CurrentHost = $script:CurrentHost

                Export-IBWAPIConfig $coldConfig
            }

        }

    }




    <#
    .SYNOPSIS
        Remove WAPI configuration data from memory and disk.
 
    .DESCRIPTION
        When calling this function with no parameters, the currently active set of config values will be removed.
 
        When called with -WAPIHost, the set of config values for that host will be removed.
 
        When called with -AllHosts, all sets of config values will be removed.
 
    .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 removed.
 
    .EXAMPLE
        Remove-IBWAPIConfig
 
        Removes the currently active WAPI config set from memory and disk.
 
    .EXAMPLE
        Remove-IBWAPIConfig -AllHosts
 
        Removes all config sets from memory and disk.
 
    .LINK
        Project: https://github.com/rmbolger/Posh-IBWAPI
 
    .LINK
        Set-IBWAPIConfig
 
    .LINK
        Save-IBWAPIConfig
 
    #>

}