Azure 服务标签 : region names in ServiceTags_Public_*. json 与 Get-AzLocation 中的区域名称不同?

标签 azure powershell azure-devops azure-powershell azure-virtual-network

我希望使用经常更改的 Azure/Microsoft 服务的 IP 地址来自动更新防火墙入站 ACL 的过程。因此,我经常需要这些服务使用的子网/地址前缀的最新列表。大多数时候,我可以使用 Get-AzNetworkServiceTag 在 PowerShell 中顺利工作。但在这种情况下,我需要 Azure DevOps Microsoft 托管代理的 IP,并且它们不包含在 Get-AzNetworkServiceTag 中,如 this article 中所述。 。所以,我必须使用 ServiceTags JSON-file Microsoft 发布的。

从技术上讲,没什么大不了的:只是从 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 = centralfrancefrancesouth = southfrancenorwayeast = norwaye

但是ukwest = ukwestwesteurope = westeurope所以它们并非全无用......

显然,我需要这些值相同才能完成自动化工作。有谁知道如何做到这一点?我是否在寻找错误的位置 (Get-AzLocation) 来进行正确的映射?

最佳答案

我修改了您的脚本如下以达到要求。

最初,我将所有区域映射存储在一个名为 mapping 的数组中,然后如图所示获取它们。完成后,您可以从中检索对象地址前缀

$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

输出:

enter image description here

关于Azure 服务标签 : region names in ServiceTags_Public_*. json 与 Get-AzLocation 中的区域名称不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77201895/

相关文章:

git - 如何通过 ssh 获取托管在 Azure DevOps Repo 中的 Terraform 模块

sql-server - 如何在插入时从 Azure Sql 数据库获取通知

azure - 文件转换任务无法转换压缩包上的 XML 配置

powershell - PowerShell 中的 Word.Application ComObject 错误

c# - 如何通过 Angularjs 应用程序从浏览器获取 Windows 凭据并在服务级别使用它们?

c# - 使用 Azure Devops Rest API 的访问 token

azure - Redis 缓存 maxmemory-reserved 设置是 Azure 独有的吗?

entity-framework - 使用 Entity Framework Code First 在 Azure 中创建基本版本数据库

python - 从 PowerShell 运行 python 源文件

date - 如何使用 PowerShell 将 'date-1' 格式化为 mm-dd-yyyy?