如何在预先存在的 Vnet Powershell 中创建多个子网

How to create multiple Subnets in pre-existing Vnet Powershell

我有以下代码可以很好地添加一些子网,但我希望此脚本利用 CSV 文件从预先存在的 Vnet 导入子网并将子网添加到预先存在的 Vnet 中?



$appssubnet = New-AzVirtualNetworkSubnetConfig -Name servers -AddressPrefix "172.16.1.0/24" -NetworkSecurityGroupId "/subscriptions/xxxxxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxxxxxx/resourceGroups/powershell-grp/providers/Microsoft.Network/networkSecurityGroups/app-nsg1"`
-RouteTableId "/subscriptions/xxxxxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxxxxxx/resourceGroups/powershell-grp/providers/Microsoft.Network/routeTables/powershell-rt"
$serversubnet = New-AzVirtualNetworkSubnetConfig -Name apps -AddressPrefix "172.16.2.0/24" -RouteTableId "/subscriptions/xxxxxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxxxxxx/resourceGroups/powershell-grp/providers/Microsoft.Network/routeTables/powershell-rt"
$dmz = New-AzVirtualNetworkSubnetConfig -Name dmz -AddressPrefix "172.16.3.0/24"
$updatedvnet = New-AzVirtualNetwork -Name "testsubnet" -ResourceGroupName "powershell-grp" -Location "North Europe" -AddressPrefix "172.16.0.0/16" -Subnet $serversubnet, $dmz, $appssubnet -Force:$true
$updatedvnet | Set-AzVirtualNetwork


您可以使用下面的 powershell 脚本。

  $subnets1 = Import-Csv "C:\Users\v-XXXsXX18\Documents\TestCount.csv"
    #$subnets1.subnetName
    foreach ($subnet in $subnets1){
    $dmz = New-AzVirtualNetworkSubnetConfig -Name $subnet.SubnetName -AddressPrefix $subnet.AddressPrefix
    
    $vnet=Get-AzVirtualNetwork -Name "MyVirtualNetworkTes" -ResourceGroupName "v-raXXXXndtree"
    $updatedvnet=Add-AzVirtualNetworkSubnetConfig -Name $dmz.Name -VirtualNetwork $vnet -AddressPrefix $dmz.AddressPrefix
    
    $updatedvnet | Set-AzVirtualNetwork
    
    
    }

输出--