[POWERSHELL] Create DHCP Reservation in FortiGate Firewall

Issue

To provision static DHCP mappings for 500 phones, I am using a CSV file with the fields ROOM, PHONENUMBER, MACADDRESS, and IPADDRESS. FortiGate does not support bulk creation of DHCP reservations through its UI, so I created a PowerShell script that parses the CSV and outputs the corresponding FortiGate CLI configuration statements.

FortiGate Basic Config

config system dhcp server
   edit 1
      set default-gateway 192.168.1.1
      set netmask 255.255.255.0
      set interface "lan"
      config ip-range
         edit 1
            set start-ip 192.168.1.100
            set end-ip 192.168.1.200
         next
      end
      config reserved-address
         edit 1
            set ip 192.168.1.50
            set mac aa:bb:cc:dd:ee:ff
         next
      end
      next
end

Powershell Script for config reserved-address

$startC = 1

foreach ($ip in $ips){
   $clean = ($ip.MACADDRESS -replace '[^0-9A-Fa-f]', '')
   if ($clean.Length -ne 12) {
      throw "Invalid MAC address format: '$Mac'"
   }
   
   # Insert colons every 2 characters and convert to lowercase
   $formattedMACADDRESS = ($clean.ToLower() -split '(.{2})' | Where-Object { $_ -ne '' }) -join ':'
   write-host @"
   edit $($startC)
      set ip $($ip.IPADDRESS)
      set mac $($formattedMACADDRESS)
      set description "Room $($ip.ROOM) - $($ip.PHONENUMBER)"
   next
"@
$startC++
}

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest Comments

No comments to show.