PowerShell Script to add Azure IP Ranges and Service Tags as address list

I just wanted to share this one in case anyone needs it.

Use Case

Example Entry of ServiceTags_Public_20241007.json:

{
      "name": "GatewayManager.CentralUS",
      "id": "GatewayManager.CentralUS",
      "properties": {
        "changeNumber": 6,
        "region": "centralus",
        "regionId": 31,
        "platform": "Azure",
        "systemService": "GatewayManager",
        "addressPrefixes": [
          "13.89.171.96/29",
          "20.37.152.72/29",
          "20.98.130.16/32",
          "20.118.195.160/27",
          "52.143.250.137/32",
          "52.143.251.22/32",
          "2603:1030:10:1::40/122"
        ],
        "networkFeatures": [
          "NSG"
        ]
      }
    }

PowerShell Script: https://gist.github.com/florian-obradovic/43c451e336fd4f2bf6f2c342fec64e4a

# Load the JSON file
$jsonData = Get-Content -Path "/Users/flo/Downloads/ServiceTags_Public_20241007.json" -Raw | ConvertFrom-Json

# Filter the data to get addressPrefixes where systemService is "GatewayManager" and region contains "germany" or "europe"
$addressPrefixes = $jsonData.values | Where-Object { 
    $_.properties.systemService -eq "GatewayManager" -and 
    ($_.properties.region -match "germany" -or $_.properties.region -match "europe")
} | Select-Object -ExpandProperty properties | Select-Object -ExpandProperty addressPrefixes

# Output the addressPrefixes
$addressPrefixes
foreach ($address in $addressPrefixes) {
    if ($address -match ":") {
        Write-Host "/ipv6/firewall/address-list/add address=""$address"" list=""Microsoft-Azure-GatewayManager"" comment=""Microsoft-Azure-GatewayManager TI-9964"""
    } else {
        Write-Host "/ip/firewall/address-list/add address=""$address"" list=""Microsoft-Azure-GatewayManager"" comment=""Microsoft-Azure-GatewayManager TI-9964"""
    }
}