From 39beac970e26117ba9f5ac41a986ca7db25b17da Mon Sep 17 00:00:00 2001 From: Alexis La Goutte Date: Sat, 4 May 2024 21:00:37 +0200 Subject: [PATCH] DHCP(System): Add Get-FGTSystemDHCPServer For get list of DHCP Server configured on fortigate Ping: #244 --- PowerFGT/Public/cmdb/system/dhcp-server.ps1 | 99 +++++++++++++++++++++ Tests/integration/Connection.Tests.ps1 | 3 + 2 files changed, 102 insertions(+) create mode 100644 PowerFGT/Public/cmdb/system/dhcp-server.ps1 diff --git a/PowerFGT/Public/cmdb/system/dhcp-server.ps1 b/PowerFGT/Public/cmdb/system/dhcp-server.ps1 new file mode 100644 index 000000000..7f6ed7c64 --- /dev/null +++ b/PowerFGT/Public/cmdb/system/dhcp-server.ps1 @@ -0,0 +1,99 @@ +# +# Copyright 2022, Alexis La Goutte +# +# SPDX-License-Identifier: Apache-2.0 +# + +function Get-FGTSystemDHCPServer { + + <# + .SYNOPSIS + Get DHCP Server configured + + .DESCRIPTION + Show DHCP Server configured on the FortiGate + + .EXAMPLE + Get-FGTSystemDHCPServer + + Display DHCP Server configured on the FortiGate + + .EXAMPLE + Get-FGTSystemDHCPServer -filter_attribute 'default-gateway' -filter_value 92.2.0.254 + + Get System DHCP Server with default-gateway equal 192.2.0.254 + + .EXAMPLE + Get-FGTSystemDHCPServer -filter_attribute domain -filter_value PowerFGT -filter_type contains + + Get System Server DHCP with domain contains PowerFGT + + .EXAMPLE + Get-FGTSystemDHCPServer -meta + + Display DHCP Server configured on the FortiGate with metadata (q_...) like usage (q_ref) + + .EXAMPLE + Get-FGTSystemDHCPServer -skip + + Display DHCP Server configured on the FortiGate (but only relevant attributes) + + EXAMPLE + Get-FGTSystemDHCPServer -vdom vdomX + + Display DHCP Server configured on the FortiGate on vdomX + #> + + [CmdletBinding(DefaultParameterSetName = "default")] + Param( + [Parameter (Mandatory = $false)] + [Parameter (ParameterSetName = "filter")] + [string]$filter_attribute, + [Parameter (Mandatory = $false)] + [Parameter (ParameterSetName = "filter")] + [ValidateSet('equal', 'contains')] + [string]$filter_type = "equal", + [Parameter (Mandatory = $false)] + [Parameter (ParameterSetName = "filter")] + [psobject]$filter_value, + [Parameter(Mandatory = $false)] + [switch]$meta, + [Parameter(Mandatory = $false)] + [switch]$skip, + [Parameter(Mandatory = $false)] + [String[]]$vdom, + [Parameter(Mandatory = $false)] + [psobject]$connection = $DefaultFGTConnection + ) + + Begin { + } + + Process { + + $invokeParams = @{ } + if ( $PsBoundParameters.ContainsKey('meta') ) { + $invokeParams.add( 'meta', $meta ) + } + if ( $PsBoundParameters.ContainsKey('skip') ) { + $invokeParams.add( 'skip', $skip ) + } + if ( $PsBoundParameters.ContainsKey('vdom') ) { + $invokeParams.add( 'vdom', $vdom ) + } + + #Filtering + #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-FGTRestMethod -uri 'api/v2/cmdb/system.dhcp/server' -method 'GET' -connection $connection @invokeParams + $response.results + } + + End { + } +} \ No newline at end of file diff --git a/Tests/integration/Connection.Tests.ps1 b/Tests/integration/Connection.Tests.ps1 index de0626d8e..c2e7349be 100644 --- a/Tests/integration/Connection.Tests.ps1 +++ b/Tests/integration/Connection.Tests.ps1 @@ -202,6 +202,9 @@ Describe "Connect to a FortiGate (using multi connection)" { It "Use Multi connection for call Get System Admin(istrator)" { { Get-FGTSystemAdmin -connection $fgt } | Should -Not -Throw } + It "Use Multi connection for call Get System DHCP Server" { + { Get-FGTSystemDHCPServer -connection $fgt } | Should -Not -Throw + } It "Use Multi connection for call Get System DNS" { { Get-FGTSystemDns -connection $fgt } | Should -Not -Throw }