我想使用经常更改的 Azure/Microsoft 服务的 IP 地址来自动更新防火墙入站 ACL 的过程。因此,我经常需要这些服务使用的子网/地址前缀的最新列表。大多数时候,我都可以在 PowerShell 中使用
Get-AzNetworkServiceTag
。但在这种情况下,我需要 Azure DevOps Microsoft 托管代理的 IP,并且它们不包含在 Get-AzNetworkServiceTag
中,如本文中所述。因此,我必须使用 Microsoft 发布的 ServiceTags JSON 文件。
从技术上讲,没什么大不了的:只是从 JSON 文件而不是 cmdlet 的输出中获取内容。如果不是 JSON 文件包含意外值。
当使用
Get-AzLocation
确定我的地理位置中的区域时,将返回:
PS C:\> Get-AzLocation | Where-Object {$_.GeographyGroup -eq 'Europe'} | Select-Object -ExpandProperty 'Location'
northeurope
swedencentral
uksouth
westeurope
francecentral
germanywestcentral
italynorth
norwayeast
polandcentral
switzerlandnorth
francesouth
germanynorth
norwaywest
switzerlandwest
ukwest
但是 ServiceTags_Public_20230925.json 使用不同的值。例如:
# Gets latest list of Azure IP ranges from Microsoft
$JsonFileUrl = 'https://download.microsoft.com/download/7/1/D/71D86715-5596-4529-9B13-DA13A5DE5B63/ServiceTags_Public_20230925.json'
$JsonFileName = "ServiceTags_Public_20230925.json"
# Download file
$null = Invoke-WebRequest -Uri $JsonFileUrl -OutFile $JsonFileName
# Parse file as JSON-object
$Json = Get-Content -LiteralPath $JsonFileName -Raw | ConvertFrom-Json
# Return unique region names in geography 'Europe'
$Json.values.properties | Where-Object {$_.region -like '*germany*'} | Select-Object -ExpandProperty region -Unique | Where-Object -Property region -like '*germany*' | Select-Object -ExpandProperty region -Unique
# Result:
germanyn
germanywc
因此 Get-AzLocation 中的
germanywestcentral
似乎是 JSON 文件中的 germanywc
。而且:germanynorth
等于 germanyn
。并且 francecentral
= centralfrance
和 francesouth
= southfrance
和 norwayeast
= norwaye
等等
但是
ukwest
= ukwest
和 westeurope
= westeurope
所以它们并非全无用...
显然,我需要这些值相同才能完成自动化工作。有谁知道如何做到这一点?我是否在寻找错误的位置 (Get-AzLocation) 来进行正确的映射?
我修改了你的脚本如下以达到要求。
最初,我将所有区域映射存储在一个名为
mapping
的数组中,然后如图所示获取它们。完成后,您可以从中检索对象address prefixes
。
$JsonFileUrl = 'https://download.microsoft.com/download/7/1/D/71D86715-5596-4529-9B13-DA13A5DE5B63/ServiceTags_Public_20230925.json'
$JsonFileName = "ServiceTags_Public_20230925.json"
$null = Invoke-WebRequest -Uri $JsonFileUrl -OutFile $JsonFileName
$Json = Get-Content -LiteralPath $JsonFileName -Raw | ConvertFrom-Json
$mapping = @{
'germanywestcentral' = 'germanywc'
'germanynorth' = 'germanyn'
'francecentral' = 'centralfrance' #Add all the remaining locations in the same way
}
$regiontranslate = $mapping['germanynorth']
$Json.values.properties | Where-Object {$_.region -like $regiontranslate} | Select-Object -ExpandProperty addressprefixes -Unique
输出: