Public/CLI.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
#
# 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-ArubaSWCli {

    <#
        .SYNOPSIS
        Get the result of a cli command on ArubaOS Switch.
 
        .DESCRIPTION
        Get the result of a cli command.
 
        All configuration and execution commands in non-interactive mode are supported.
        crypto, copy, process-tracking, recopy, redo, repeat, session, end, print, terminal,
        logout, menu, page, restore, update, upgrade-software, return, setup, screen-length,
        vlan range and help commands are not supported.
        Testmode commands are not supported.
        All show commands are supported except show tech and show history
 
        .EXAMPLE
        Get-ArubaSWCli -cmd "Show running-config"
 
        This function give you the result (cmd, status, result, error_mesg...) of a cli command on the switch.
 
        .EXAMPLE
        Get-ArubaSWCli -cmd "Show running-config" -display_result
 
        This function give only ther esult of a cli command on the switch.
    #>


    Param(
        [Parameter (Mandatory = $true, Position = 1)]
        [string]$cmd,
        [Parameter (Mandatory = $false)]
        [switch]$display_result,
        [Parameter (Mandatory=$False)]
        [ValidateNotNullOrEmpty()]
        [PSObject]$connection=$DefaultArubaSWConnection
    )

    Begin {
    }

    Process {

        $uri = "rest/v4/cli"

        $run = New-Object -TypeName PSObject

        $run | Add-Member -name "cmd" -membertype NoteProperty -Value "$cmd"

        $response = Invoke-ArubaSWWebRequest -method "POST" -body $run -uri $uri -connection $connection

        $conf = ($response | ConvertFrom-Json)

        $result_base64_encoded = $conf.result_base64_encoded

        $result = [System.Convert]::FromBase64String($result_base64_encoded)

        $result = [System.Text.Encoding]::UTF8.GetString($result)

        $conf | Add-Member -name "result" -membertype NoteProperty -value $result

        if ($display_result) {
            #only display CLI output
            $conf.result
        }
        else {
            $conf
        }
    }

    End {
    }
}

function Send-ArubaSWCliBatch {

    <#
        .SYNOPSIS
        Send a cli batch command.
 
        .DESCRIPTION
        Send a cli batch command on Aruba OS Switch.
        All configuration commands in non-interactive mode are supported. Exit, configure, erase, startup-config commands are supported.
        Crypto, show, execution and testmode commands are NOT supported.
 
        .EXAMPLE
        Send-ArubaSWCliBatch -command "interface 4 disable"
 
        Send a cli batch command (disable interface 4) on the switch, use Get-ArubaSWCliBatchStatus for get result
 
        .EXAMPLE
        Send-ArubaSWCliBatch -command "interface 4", "enable", "name PowerArubaSW-int"
 
        Send a cli batch command (disable interface 4) on the switch, use Get-ArubaSWCliBatchStatus for get result
        #>


    Param(
        [Parameter (Mandatory = $true)]
        [string[]]$command,
        [Parameter (Mandatory=$False)]
        [ValidateNotNullOrEmpty()]
        [PSObject]$connection=$DefaultArubaSWConnection
    )

    Begin {
    }

    Process {

        $nb = 0

        foreach ($line in $command) {
            $result = $result + $command[$nb] + "`n"
            $nb = $nb + 1
        }

        $uri = "rest/v4/cli_batch"

        $conf = New-Object -TypeName PSObject

        $encode = [System.Text.Encoding]::UTF8.GetBytes($result)

        $EncodedText = [Convert]::ToBase64String($encode)

        $conf | Add-Member -name "cli_batch_base64_encoded" -membertype NoteProperty -Value $EncodedText

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

        $run = $response | ConvertFrom-Json

        $run
    }

    End {
    }
}

function Get-ArubaSWCliBatchStatus {

    <#
        .SYNOPSIS
        Get a cli batch command status.
 
        .DESCRIPTION
        Get a cli batch command status on Aruba OS Switch.
 
        .EXAMPLE
        Get-ArubaSWCliBatchStatus
 
        Get a cli batch command status on the switch.
    #>


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

    Begin {
    }

    Process {

        $uri = "rest/v4/cli_batch/status"

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

        $run = ($response | ConvertFrom-Json).cmd_exec_logs

        $run
    }

    End {
    }
}