Public/Endpoint.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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# # Copyright 2019, Alexis La Goutte <alexis.lagoutte at gmail dot com> # # SPDX-License-Identifier: Apache-2.0 # function Add-ArubaCPEndpoint { <# .SYNOPSIS Add an Endpoint on ClearPass .DESCRIPTION Add an Endpoint with mac address, description, status, attributes .EXAMPLE Add-ArubaCPEndpoint -mac_address 000102030405 -description "Add by PowerArubaCP" -Status Known Add an Endpoint with MAC Address 000102030405 with Known Status and a description .EXAMPLE Add-ArubaCPEndpoint -mac_address 00:01:02:03:04:06 -status Unknown Add an Endpoint with MAC Address 00:01:02:03:04:06 with Unknown Status .EXAMPLE $attributes = @{"Disabled by"="PowerArubaCP"} PS >Add-ArubaCPEndpoint -mac_address 000102-030407 -Status Disabled -attributes $attributes Add an Endpoint with MAC Address 000102030405 with Disabled Status and attributes to Disabled by PowerArubaCP #> Param( [Parameter (Mandatory = $false)] [int]$id, [Parameter (Mandatory = $true)] [string]$mac_address, [Parameter (Mandatory = $false)] [string]$description, [Parameter (Mandatory = $true)] [ValidateSet('Known', 'Unknown', 'Disabled', IgnoreCase = $false)] [string]$status, [Parameter (Mandatory = $false)] [psobject]$attributes, [Parameter (Mandatory = $False)] [ValidateNotNullOrEmpty()] [PSObject]$connection = $DefaultArubaCPConnection ) Begin { } Process { $uri = "api/endpoint" $_ep = new-Object -TypeName PSObject if ( $PsBoundParameters.ContainsKey('id') ) { $_ep | add-member -name "id" -membertype NoteProperty -Value $id } $_ep | add-member -name "mac_address" -membertype NoteProperty -Value (Format-ArubaCPMacAddress $mac_address) if ( $PsBoundParameters.ContainsKey('description') ) { $_ep | add-member -name "description" -membertype NoteProperty -Value $description } $_ep | add-member -name "status" -membertype NoteProperty -Value $status if ( $PsBoundParameters.ContainsKey('attributes') ) { $_ep | add-member -name "attributes" -membertype NoteProperty -Value $attributes } $ep = invoke-ArubaCPRestMethod -method "POST" -body $_ep -uri $uri -connection $connection $ep } End { } } function Get-ArubaCPEndpoint { <# .SYNOPSIS Get Endpoint info on CPPM .DESCRIPTION Get Endpoint (Id, MAC Address, Status, Attributes) .EXAMPLE Get-ArubaCPEndpoint Get ALL Endpoint on the Clearpass .EXAMPLE Get-ArubaCPEndpoint 000102030405 Get info about Endpoint 000102030405 Aruba on the ClearPass .EXAMPLE Get-ArubaCPEndpoint -id 23 Get info about Endpoint id 23 on the ClearPass .EXAMPLE Get-ArubaCPEndpoint 000102030405 -filter_type contains Get info about Endpoint where name contains 000102 .EXAMPLE Get-ArubaCPEndpoint -filter_attribute attribute -filter_type contains -filter_value 192.0.2.1 Get info about Endpoint where attribute contains 192.0.2.1 #> [CmdLetBinding(DefaultParameterSetName = "Default")] Param( [Parameter (Mandatory = $false)] [Parameter (ParameterSetName = "id")] [int]$id, [Parameter (Mandatory = $false, Position = 1)] [Parameter (ParameterSetName = "mac_address")] [string]$mac_address, [Parameter (Mandatory = $false)] [Parameter (ParameterSetName = "status")] [ValidateSet('Known', 'Unknown', 'Disabled')] [string]$status, [Parameter (Mandatory = $false)] [Parameter (ParameterSetName = "filter")] [string]$filter_attribute, [Parameter (Mandatory = $false)] [Parameter (ParameterSetName = "id")] [Parameter (ParameterSetName = "mac_address")] [Parameter (ParameterSetName = "status")] [Parameter (ParameterSetName = "filter")] [ValidateSet('equal', 'contains')] [string]$filter_type, [Parameter (Mandatory = $false)] [Parameter (ParameterSetName = "filter")] [psobject]$filter_value, [Parameter (Mandatory = $false)] [int]$limit, [Parameter (Mandatory = $False)] [ValidateNotNullOrEmpty()] [PSObject]$connection = $DefaultArubaCPConnection ) Begin { } Process { $invokeParams = @{ } if ( $PsBoundParameters.ContainsKey('limit') ) { $invokeParams.add( 'limit', $limit ) } switch ( $PSCmdlet.ParameterSetName ) { "id" { $filter_value = $id $filter_attribute = "id" } "mac_address" { $filter_value = Format-ArubaCPMACAddress $mac_address $filter_attribute = "mac_address" } "status" { $filter_value = $status $filter_attribute = "status" } default { } } if ( $PsBoundParameters.ContainsKey('filter_type') ) { switch ( $filter_type ) { "equal" { $filter_value = @{ "`$eq" = $filter_value } } "contains" { $filter_value = @{ "`$contains" = $filter_value } } default { } } } if ($filter_value -and $filter_attribute) { $filter = @{ $filter_attribute = $filter_value } $invokeParams.add( 'filter', $filter ) } $uri = "api/endpoint" $endpoint = Invoke-ArubaCPRestMethod -method "GET" -uri $uri @invokeParams -connection $connection $endpoint._embedded.items } End { } } function Set-ArubaCPEndpoint { <# .SYNOPSIS Configure an Endpoint on ClearPass .DESCRIPTION Configure an Endpoint on ClearPass .EXAMPLE $ep = Get-ArubaCPEndpoint -mac_address 00:01:02:03:04:05 PS C:\>$ep | Set-ArubaCPEndpoint -status Disabled Set Status Disabled for MAC Address 00:01:02:03:04:05 .EXAMPLE $ep = Get-ArubaCPEndpoint -mac_address 00:01:02:03:04:05 PS C:\>$ep | Set-ArubaCPEndpoint -description "Change by PowerAruba" Change Description for MAC Address 00:01:02:03:04:05 .EXAMPLE $ep = Get-ArubaCPEndpoint -mac_address 00:01:02:03:04:05 PS C:\>$ep | Set-ArubaCPEndpoint -mac_address 00:01:02:03:04:06 Change MAC Address 00:01:02:03:04:05 => 00:01:02:03:04:06 #> [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'medium')] Param( [Parameter (Mandatory = $true, ParameterSetName = "id")] [int]$id, [Parameter (Mandatory = $true, ValueFromPipeline = $true, Position = 1, ParameterSetName = "ep")] [ValidateScript( { Confirm-ArubaCPEndpoint $_ })] [psobject]$ep, [Parameter (Mandatory = $false)] [string]$mac_address, [Parameter (Mandatory = $false)] [string]$description, [Parameter (Mandatory = $false)] [ValidateSet('Known', 'Unknown', 'Disabled', IgnoreCase = $false)] [string]$status, [Parameter (Mandatory = $False)] [ValidateNotNullOrEmpty()] [PSObject]$connection = $DefaultArubaCPConnection ) Begin { } Process { #get nad id from nad ps object if ($ep) { $id = $ep.id } $uri = "api/endpoint/${id}" $_ep = new-Object -TypeName PSObject if ( $PsBoundParameters.ContainsKey('id') ) { $_ep | add-member -name "id" -membertype NoteProperty -Value $id } if ( $PsBoundParameters.ContainsKey('mac_address') ) { $_ep | add-member -name "mac_address" -membertype NoteProperty -Value $mac_address } if ( $PsBoundParameters.ContainsKey('description') ) { $_ep | add-member -name "description" -membertype NoteProperty -Value $description } if ( $PsBoundParameters.ContainsKey('status') ) { $_ep | add-member -name "status" -membertype NoteProperty -Value $status } if ($PSCmdlet.ShouldProcess($id, 'Configure Endpoint')) { $ep = Invoke-ArubaCPRestMethod -method "PATCH" -body $_ep -uri $uri -connection $connection $ep } } End { } } function Remove-ArubaCPEndpoint { <# .SYNOPSIS Remove an Endpoint on ClearPass .DESCRIPTION Remove an Endpoint on ClearPass .EXAMPLE $ep = Get-ArubaCPEndpoint -mac_address 00:01:02:03:04:05 PS C:\>$ep | Remove-ArubaCPEndpoint Remove an Endpoint with MAC Address 00:01:02:03:04:05 .EXAMPLE Remove-ArubaCPEndpoint -id 3001 -confirm:$false Remove an Endpoint with id 3001 and no confirmation #> [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'high')] Param( [Parameter (Mandatory = $true, ParameterSetName = "id")] [int]$id, [Parameter (Mandatory = $true, ValueFromPipeline = $true, Position = 1, ParameterSetName = "ep")] [ValidateScript( { Confirm-ArubaCPEndpoint $_ })] [psobject]$ep, [Parameter (Mandatory = $False)] [ValidateNotNullOrEmpty()] [PSObject]$connection = $DefaultArubaCPConnection ) Begin { } Process { #get endpoint id from ep ps object if ($ep) { $id = $ep.id $mac = "(" + $ep.mac_address + ")" } $uri = "api/endpoint/${id}" if ($PSCmdlet.ShouldProcess("$id $mac", 'Remove Endpoint')) { Invoke-ArubaCPRestMethod -method "DELETE" -uri $uri -connection $connection } } End { } } |