Public/pm/firewall/address.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 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 |
# # Copyright 2021, Alexis La Goutte <alexis dot lagoutte at gmail dot com> # Copy from PowerFGT.. :) # # SPDX-License-Identifier: Apache-2.0 # function Add-FMGFirewallAddress { <# .SYNOPSIS Add a Firewall Address .DESCRIPTION Add a Firewall Address (ipmask, iprange, fqdn) .EXAMPLE Add-FMGFirewallAddress -Name FMG -ip 192.0.2.0 -mask 255.255.255.0 Add Address object type ipmask with name FMG and value 192.0.2.0/24 .EXAMPLE Add-FMGFirewallAddress -Name FMG -ip 192.0.2.0 -mask 255.255.255.0 -interface port2 Add Address object type ipmask with name FMG, value 192.0.2.0/24 and associated to interface port2 .EXAMPLE Add-FMGFirewallAddress -Name FMG -ip 192.0.2.0 -mask 255.255.255.0 -comment "My FMG Address" Add Address object type ipmask with name FMG, value 192.0.2.0/24 and a comment .EXAMPLE Add-FMGFirewallAddress -Name FMG -ip 192.0.2.0 -mask 255.255.255.0 -visibility:$false Add Address object type ipmask with name FMG, value 192.0.2.0/24 and disabled visibility .EXAMPLE Add-FMGFirewallAddress -Name FortiPower -fqdn fortipower.github.io Add Address object type fqdn with name FortiPower and value fortipower.github.io .EXAMPLE Add-FMGFirewallAddress -Name FMG-Range -startip 192.0.2.1 -endip 192.0.2.100 Add Address object type iprange with name FMG-Range with start IP 192.0.2.1 and end ip 192.0.2.100 #> Param( [Parameter (Mandatory = $false, DontShow)] [Obsolete("Use -ip or -fqdn instead")] [string]$type, [Parameter (Mandatory = $true)] [string]$name, [Parameter (Mandatory = $false, ParameterSetName = "fqdn")] [string]$fqdn, [Parameter (Mandatory = $false, ParameterSetName = "ipmask")] [ipaddress]$ip, [Parameter (Mandatory = $false, ParameterSetName = "ipmask")] [ipaddress]$mask, [Parameter (Mandatory = $false, ParameterSetName = "iprange")] [ipaddress]$startip, [Parameter (Mandatory = $false, ParameterSetName = "iprange")] [ipaddress]$endip, [Parameter (Mandatory = $false)] [string]$interface, [Parameter (Mandatory = $false)] [ValidateLength(0, 255)] [string]$comment, [Parameter (Mandatory = $false)] [boolean]$visibility, [Parameter(Mandatory = $false)] [psobject]$connection = $DefaultFMGConnection ) Begin { } Process { $invokeParams = @{ } if ( Get-FMGFirewallAddress @invokeParams -name $name -connection $connection) { Throw "Already an address object using the same name" } $uri = "firewall/address" $address = new-Object -TypeName PSObject $address | add-member -name "name" -membertype NoteProperty -Value $name switch ( $PSCmdlet.ParameterSetName ) { "ipmask" { $address | add-member -name "type" -membertype NoteProperty -Value "ipmask" $subnet = $ip.ToString() $subnet += "/" $subnet += $mask.ToString() $address | add-member -name "subnet" -membertype NoteProperty -Value $subnet } "iprange" { $address | add-member -name "type" -membertype NoteProperty -Value "iprange" $address | add-member -name "start-ip" -membertype NoteProperty -Value $startip.ToString() $address | add-member -name "end-ip" -membertype NoteProperty -Value $endip.ToString() } "fqdn" { $address | add-member -name "type" -membertype NoteProperty -Value "fqdn" $address | add-member -name "fqdn" -membertype NoteProperty -Value $fqdn } default { } } if ( $PsBoundParameters.ContainsKey('interface') ) { #TODO check if the interface (zone ?) is valid $address | add-member -name "associated-interface" -membertype NoteProperty -Value $interface } if ( $PsBoundParameters.ContainsKey('comment') ) { $address | add-member -name "comment" -membertype NoteProperty -Value $comment } if ( $PsBoundParameters.ContainsKey('visibility') ) { #with 6.4.x, there is no longer visibility parameter if ($connection.version -ge "6.4.0") { Write-Warning "-visibility parameter is no longer available with FortiOS 6.4.x and after" } else { if ( $visibility ) { $address | add-member -name "visibility" -membertype NoteProperty -Value "enable" } else { $address | add-member -name "visibility" -membertype NoteProperty -Value "disable" } } } Invoke-FMGRestMethod -method "add" -type "pm" -body $address -uri $uri -connection $connection @invokeParams | out-Null Get-FMGFirewallAddress -connection $connection @invokeParams -name $name } End { } } function Copy-FMGFirewallAddress { <# .SYNOPSIS Copy/Clone a Firewall Address .DESCRIPTION Copy/Clone a Firewall Address (ip, mask, comment, associated interface... ) .EXAMPLE $MyFMGAddress = Get-FMGFirewallAddress -name MyFMGAddress PS C:\>$MyFMGAddress | Copy-FMGFirewallAddress -name MyFMGAddress_copy Copy / Clone MyFMGAddress and name MyFMGAddress_copy #> Param( [Parameter (Mandatory = $true, ValueFromPipeline = $true, Position = 1)] [ValidateScript( { Confirm-FMGAddress $_ })] [psobject]$address, [Parameter (Mandatory = $true)] [string]$name, [Parameter(Mandatory = $false)] [psobject]$connection = $DefaultFMGConnection ) Begin { } Process { $invokeParams = @{ } $uri = "firewall/address/$($address.name)" $body = Get-FMGFirewallAddress -connection $connection @invokeParams -name $address.name $body.name = $name Invoke-FMGRestMethod -method "clone" -type "pm" -uri $uri -body $body -connection $connection @invokeParams | out-Null Get-FMGFirewallAddress -connection $connection @invokeParams -name $name } End { } } function Get-FMGFirewallAddress { <# .SYNOPSIS Get list of all "address" .DESCRIPTION Get list of all "address" (ipmask, iprange, fqdn...) .EXAMPLE Get-FMGFirewallAddress Get list of all address object .EXAMPLE Get-FMGFirewallAddress -name myFMGAddress Get address named myFMGAddress .EXAMPLE Get-FMGFirewallAddress -name FMG -filter_type like Get address like with %FMG% .EXAMPLE Get-FMGFirewallAddress -uuid 9e73a10e-1772-51ea-a8d7-297686fd7702 Get address with uuid 9e73a10e-1772-51ea-a8d7-297686fd7702 #> [CmdletBinding(DefaultParameterSetName = "default")] Param( [Parameter (Mandatory = $false, Position = 1, ParameterSetName = "name")] [string]$name, [Parameter (Mandatory = $false, ParameterSetName = "uuid")] [string]$uuid, [Parameter (Mandatory = $false)] [Parameter (ParameterSetName = "filter")] [string]$filter_attribute, [Parameter (Mandatory = $false)] [Parameter (ParameterSetName = "name")] [Parameter (ParameterSetName = "uuid")] [Parameter (ParameterSetName = "filter")] [ValidateSet('equal', 'contains', 'like')] [string]$filter_type = "equal", [Parameter (Mandatory = $false)] [Parameter (ParameterSetName = "filter")] [psobject]$filter_value, [Parameter(Mandatory = $false)] [psobject]$connection = $DefaultFMGConnection ) Begin { } Process { $invokeParams = @{ } switch ( $PSCmdlet.ParameterSetName ) { "name" { $filter_value = $name $filter_attribute = "name" } "uuid" { $filter_value = $uuid $filter_attribute = "uuid" } default { } } #if filter value and filter_attribute, add filter (by default filter_type is equal) if ( $filter_value -and $filter_attribute ) { $invokeParams.add( 'filter_value', $filter_value ) $invokeParams.add( 'filter_attribute', $filter_attribute ) $invokeParams.add( 'filter_type', $filter_type ) } $response = Invoke-FMGRestMethod -uri 'firewall/address' -type 'pm' -method 'get' -connection $connection @invokeParams $response } End { } } function Set-FMGFirewallAddress { <# .SYNOPSIS Configure a Firewall Address .DESCRIPTION Change a Firewall Address (ip, mask, comment, associated interface... ) .EXAMPLE $MyFMGAddress = Get-FMGFirewallAddress -name MyFMGAddress PS C:\>$MyFMGAddress | Set-FMGFirewallAddress -ip 192.0.2.0 -mask 255.255.255.0 Change MyFMGAddress to value (ip and mask) 192.0.2.0/24 .EXAMPLE $MyFMGAddress = Get-FMGFirewallAddress -name MyFMGAddress PS C:\>$MyFMGAddress | Set-FMGFirewallAddress -ip 192.0.2.1 Change MyFMGAddress to value (ip) 192.0.2.1 .EXAMPLE $MyFMGAddress = Get-FMGFirewallAddress -name MyFMGAddress PS C:\>$MyFMGAddress | Set-FMGFirewallAddress -interface port1 Change MyFMGAddress to set associated interface to port 1 .EXAMPLE $MyFMGAddress = Get-FMGFirewallAddress -name MyFMGAddress PS C:\>$MyFMGAddress | Set-FMGFirewallAddress -comment "My FMG Address" -visibility:$false Change MyFMGAddress to set a new comment and disabled visibility .EXAMPLE $MyFMGAddress = Get-FMGFirewallAddress -name MyFMGAddress PS C:\>$MyFMGAddress | Set-FMGFirewallAddress -fqdn fortipower.github.io Change MyFMGAddress to set a new fqdn fortipower.github.io .EXAMPLE $MyFMGAddress = Get-FMGFirewallAddress -name MyFMGAddress PS C:\>$MyFMGAddress | Set-FMGFirewallAddress -startip 192.0.2.100 Change MyFMGAddress to set a new startip (iprange) 192.0.2.100 .EXAMPLE $MyFMGAddress = Get-FMGFirewallAddress -name MyFMGAddress PS C:\>$MyFMGAddress | Set-FMGFirewallAddress -endip 192.0.2.200 Change MyFMGAddress to set a new endip (iprange) 192.0.2.200 #> [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'medium', DefaultParameterSetName = 'default')] Param( [Parameter (Mandatory = $true, ValueFromPipeline = $true, Position = 1)] [ValidateScript( { Confirm-FMGAddress $_ })] [psobject]$address, [Parameter (Mandatory = $false)] [string]$name, [Parameter (Mandatory = $false, ParameterSetName = "fqdn")] [string]$fqdn, [Parameter (Mandatory = $false, ParameterSetName = "ipmask")] [ipaddress]$ip, [Parameter (Mandatory = $false, ParameterSetName = "ipmask")] [ipaddress]$mask, [Parameter (Mandatory = $false, ParameterSetName = "iprange")] [ipaddress]$startip, [Parameter (Mandatory = $false, ParameterSetName = "iprange")] [ipaddress]$endip, [Parameter (Mandatory = $false)] [string]$interface, [Parameter (Mandatory = $false)] [ValidateLength(0, 255)] [string]$comment, [Parameter (Mandatory = $false)] [boolean]$visibility, [Parameter(Mandatory = $false)] [psobject]$connection = $DefaultFMGConnection ) Begin { } Process { $invokeParams = @{ } $uri = "firewall/address/$($address.name)" $_address = new-Object -TypeName PSObject if ( $PsBoundParameters.ContainsKey('name') ) { #TODO check if there is no already a object with this name ? $_address | add-member -name "name" -membertype NoteProperty -Value $name $address.name = $name } if ( $PSCmdlet.ParameterSetName -ne "default" -and $address.type -ne $PSCmdlet.ParameterSetName ) { throw "Address type ($($address.type)) need to be on the same type ($($PSCmdlet.ParameterSetName))" } switch ( $PSCmdlet.ParameterSetName ) { "ipmask" { if ( $PsBoundParameters.ContainsKey('ip') -or $PsBoundParameters.ContainsKey('mask') ) { if ( $PsBoundParameters.ContainsKey('ip') ) { $subnet = $ip.ToString() } else { $subnet = ($address.subnet -split ' ')[0] } $subnet += "/" if ( $PsBoundParameters.ContainsKey('mask') ) { $subnet += $mask.ToString() } else { $subnet += ($address.subnet -split ' ')[1] } $_address | add-member -name "subnet" -membertype NoteProperty -Value $subnet } } "iprange" { if ( $PsBoundParameters.ContainsKey('startip') ) { $_address | add-member -name "start-ip" -membertype NoteProperty -Value $startip.ToString() } if ( $PsBoundParameters.ContainsKey('endip') ) { $_address | add-member -name "end-ip" -membertype NoteProperty -Value $endip.ToString() } } "fqdn" { if ( $PsBoundParameters.ContainsKey('fqdn') ) { $_address | add-member -name "fqdn" -membertype NoteProperty -Value $fqdn } } default { } } if ( $PsBoundParameters.ContainsKey('interface') ) { #TODO check if the interface (zone ?) is valid $_address | add-member -name "associated-interface" -membertype NoteProperty -Value $interface } if ( $PsBoundParameters.ContainsKey('comment') ) { $_address | add-member -name "comment" -membertype NoteProperty -Value $comment } if ( $PsBoundParameters.ContainsKey('visibility') ) { #with 6.4.x, there is no longer visibility parameter if ($connection.version -ge "6.4.0") { Write-Warning "-visibility parameter is no longer available with FortiOS 6.4.x and after" } else { if ( $visibility ) { $_address | add-member -name "visibility" -membertype NoteProperty -Value "enable" } else { $_address | add-member -name "visibility" -membertype NoteProperty -Value "disable" } } } if ($PSCmdlet.ShouldProcess($address.name, 'Configure Firewall Address')) { Invoke-FMGRestMethod -method "set" -type 'pm' -body $_address -uri $uri -connection $connection @invokeParams | out-Null Get-FMGFirewallAddress -connection $connection @invokeParams -name $address.name } } End { } } function Remove-FMGFirewallAddress { <# .SYNOPSIS Remove a Firewall Address .DESCRIPTION Remove an address object on the Firewall .EXAMPLE $MyFMGAddress = Get-FMGFirewallAddress -name MyFMGAddress PS C:\>$MyFMGAddress | Remove-FMGFirewallAddress Remove address object $MyFMGAddress .EXAMPLE $MyFMGAddress = Get-FMGFirewallAddress -name MyFMGAddress PS C:\>$MyFMGAddress | Remove-FMGFirewallAddress -confirm:$false Remove address object $MyFMGAddress with no confirmation #> [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'high')] Param( [Parameter (Mandatory = $true, ValueFromPipeline = $true, Position = 1)] [ValidateScript( { Confirm-FMGAddress $_ })] [psobject]$address, [Parameter(Mandatory = $false)] [psobject]$connection = $DefaultFMGConnection ) Begin { } Process { $invokeParams = @{ } $uri = "firewall/address/$($address.name)" if ($PSCmdlet.ShouldProcess($address.name, 'Remove Firewall Address')) { $null = Invoke-FMGRestMethod -method "delete" -type 'pm' -uri $uri -connection $connection @invokeParams } } End { } } |