我敢肯定,您不久前已经设法解决了这个问题……但是,我有一个解决方案可以很好地解决这种问题……尽管它不使用条件语句。
在PowerShell中创建一些像这样的哈希表......
# Resource group Hashtables.
$rgDev = @{
Name = "DEV-RG"
SubscriptionId = $subNonProd
Location = "desiredregion"
}
$rgUat = @{
Name = "UAT-RG"
SubscriptionId = $subNonProd
Location = "desiredregion"
}
#Vnet Hashtables
$vnetDev = @{
ResourceGroup = $rgDev
VnetName = "Dev-vnet"
CIDR = @('x.x.x.x/27')
Subnets = @(
@{
Name = "Dev-Web-subnet"
CIDR = "y.y.y.y/28"
},
@{
Name = "Dev-DB-subnet"
CIDR = "z.z.z.z/28"
})
}
$vnetUat = @{
ResourceGroup = $rgUat
VnetName = "UAT-vnet"
CIDR = @('f.f.f.f/27')
Subnets = @(
@{
Name = "UAT-Web-subnet"
CIDR = "g.g.g.g/28"
},
@{
Name = "UAT-DB-subnet"
CIDR = "h.h.h.h/28"
})
}
接下来,我将哈希表集中到一个数组中,并在整个批次中进行 foreach。我有一个小脚本可以切换我的上下文,以便我可以在一个引导类型脚本中部署到多个订阅。
$vnets = @($vnetDev, $vnetUat)
ForEach ($vn in $vnets) {
$deploymentName = $vn.VnetName + "_Deployment."
.\SwitchSubscription.ps1 -subName $vn.ResourceGroup.SubscriptionName -subId $vn.ResourceGroup.SubscriptionId
New-AzureRmResourceGroupDeployment -Name $deploymentName `
-ResourceGroupName $vn.ResourceGroup.Name `
-TemplateFile .\JSONFiles\Vnets.json `
-vnet $vn
}
ARM 模板的参数部分如下所示...
"parameters": {
"vnet": {
"type": "object",
}
},
那么资源部分看起来像这样......
{
"name": "[concat(parameters('vnet').VnetName)]",
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2017-10-01",
"location": "[resourceGroup().location]",
"tags": "[parameters('vnet').tags]",
"properties": {
"addressSpace": {
"addressPrefixes": "[parameters('vnet').CIDR]"
},
"copy": [
{
"name": "subnets",
"count": "[length(parameters('vnet').Subnets)]",
"input": {
"name": "[parameters('vnet').Subnets[copyIndex('Subnets')].Name]",
"properties": {
"addressPrefix": "[parameters('vnet').Subnets[copyIndex('Subnets')].CIDR]"
}
}
}
]
}
}
]
所以这一切所做的就是将一个对象传递给 ARM 模板,该模板可以有一个具有一个或多个子网的 Vnet 并创建它们。
希望这将在/如果他们在谷歌搜索时发现它时帮助其他人。
干杯,
戴夫。
:)