Public/DNS.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#
# Copyright 2018, Alexis La Goutte <alexis dot lagoutte at gmail dot com>
# Copyright 2018, Cédric Moreau <moreaucedric0 at gmail dot com>
#
# SPDX-License-Identifier: Apache-2.0
#

function Get-ArubaSWDns {

    <#
        .SYNOPSIS
        Get DNS information.
 
        .DESCRIPTION
        Get DNS information about the device
 
        .EXAMPLE
        Get-ArubaSWDns
        This function give you all the informations about the dns parameters configured on the switch
    #>


    Param(
        [Parameter (Mandatory=$False)]
        [ValidateNotNullOrEmpty()]
        [PSObject]$connection=$DefaultArubaSWConnection
    )

    Begin {
    }

    Process {

        $uri = "rest/v4/dns"

        $response = Invoke-ArubaSWWebRequest -method "GET" -uri $uri -connection $connection

        $run = ($response | ConvertFrom-Json)

        $run
    }

    End {
    }
}

function Set-ArubaSWDns {

    <#
        .SYNOPSIS
        Set global configuration about DNS
 
        .DESCRIPTION
        Set DNS global parameters
 
        .EXAMPLE
        Set-ArubaSWDns -mode Manual
        Set the DNS mode to manual
 
        .EXAMPLE
        Set-ArubaSWDns -mode DHCP
        Set the DNS mode to DHCP
 
        .EXAMPLE
        Set-ArubaSWDns -mode Manual -server1 192.0.2.1 -server2 192.0.2.2 -domain example.org
        This set DNS mode to manual with server 1 and server 2 and domain name to example.org
 
        .EXAMPLE
        Set-ArubaSWDns -mode Manual -domain example.org, example.net
        This set DNS mode to manual with domain name to example.org and example.net
    #>


    Param(
        [Parameter (Mandatory = $true)]
        [ValidateSet ("DHCP", "Manual")]
        [string]$mode,
        [Parameter (Mandatory = $false)]
        [string]$server1,
        [Parameter (Mandatory = $false)]
        [string]$server2,
        [Parameter (Mandatory = $false)]
        [string[]]$domain,
        [Parameter (Mandatory=$False)]
        [ValidateNotNullOrEmpty()]
        [PSObject]$connection=$DefaultArubaSWConnection
    )

    Begin {
    }

    Process {

        $uri = "rest/v4/dns"

        $conf = New-Object -TypeName PSObject

        $ip1 = New-Object -TypeName PSObject

        $ip2 = New-Object -TypeName PSObject

        $check = Get-ArubaSWDns

        switch ( $mode ) {
            DHCP {
                $mode_status = "DCM_DHCP"
            }
            Manual {
                $mode_status = "DCM_MANUAL"
            }
        }

        $conf | Add-Member -name "dns_config_mode" -membertype NoteProperty -Value $mode_status

        if ($PsBoundParameters.ContainsKey('server1')) {
            $ip1 | Add-Member -name "version" -MemberType NoteProperty -Value "IAV_IP_V4"

            $ip1 | Add-Member -name "octets" -MemberType NoteProperty -Value $server1

            $conf | Add-Member -name "server_1" -membertype NoteProperty -Value $ip1
        }
        else {
            $conf | Add-Member -name "server_1" -membertype NoteProperty -Value $check.server_1
        }

        if ($PsBoundParameters.ContainsKey('server2')) {
            $ip2 | Add-Member -name "version" -MemberType NoteProperty -Value "IAV_IP_V4"

            $ip2 | Add-Member -name "octets" -MemberType NoteProperty -Value $server2

            $conf | Add-Member -name "server_2" -membertype NoteProperty -Value $ip2
        }
        else {
            $conf | Add-Member -name "server_2" -membertype NoteProperty -Value $check.server_2
        }

        if ( $PsBoundParameters.ContainsKey('domain') ) {
            $conf | Add-Member -name "dns_domain_names" -membertype NoteProperty -Value $domain
        }

        $response = Invoke-ArubaSWWebRequest -method "PUT" -body $conf -uri $uri -connection $connection

        $run = $response | ConvertFrom-Json

        $run
    }

    End {
    }
}

function Remove-ArubaSWDns {

    <#
        .SYNOPSIS
        Remove DNS server or domain name on the switch
 
        .DESCRIPTION
        Remove DNS server or domain name
 
        .EXAMPLE
        Remove-ArubaSWDns
        Remove the ip of server 1 and server 2, and all the domain names
    #>


    Param(
        [Parameter(Mandatory = $false)]
        [switch]$noconfirm,
        [Parameter (Mandatory=$False)]
        [ValidateNotNullOrEmpty()]
        [PSObject]$connection=$DefaultArubaSWConnection
    )

    Begin {
    }

    Process {

        $dns = New-Object -TypeName PSObject

        $dns | Add-Member -name "dns_config_mode" -membertype NoteProperty -Value "DCM_DISABLED"

        $uri = "rest/v4/dns"

        if ( -not ( $noconfirm )) {
            $message = "Remove DNS on the switch"
            $question = "Proceed with removal of DNS config ?"
            $choices = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]
            $choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes'))
            $choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&No'))

            $decision = $Host.UI.PromptForChoice($message, $question, $choices, 1)
        }
        else { $decision = 0 }
        if ($decision -eq 0) {
            Write-Progress -activity "Remove DNS"
            $null = Invoke-ArubaSWWebRequest -method "PUT" -body $dns -uri $uri -connection $connection
            Write-Progress -activity "Remove DNS" -completed
        }
    }

    End {
    }
}