我已经创建了一个创建子网的小脚本,然后可以将它们传递到循环中,并使用子网名称+ -nsg创建网络安全组(nsg),但是当我尝试将nsg关联到子网失败,表示nsg不存在。因此,我创建了两个小脚本,并从关联的nsg中拆分了创建的nsg,但同样出现了同样的错误。
错误:
Set-AzureNetworkSecurityGroupToSubnet : ResourceNotFound : The Network Security Group nsg-test-nsg does not exist.
但这确实存在。
我的代码是:
$resource = "rg-subnets"
$vnetName = "vnet-bmg"
$loc = "West Europe"
$vnet = Get-AzureRmVirtualNetwork -ResourceGroupName $resource -Name $vnetName
$sub = (Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $vnet).name
foreach ($subnet in $sub){
New-AzureRmNetworkSecurityGroup -Name $subnet-nsg -ResourceGroupName $resource -Location $loc -Force
Set-AzureNetworkSecurityGroupToSubnet -Name $subnet-nsg -VirtualNetworkName $vnetName -SubnetName $subnet
}
帮助! :)
答案 0 :(得分:0)
这对我有用,
$resource = "rg-subnets"
$vnetName = "vnet-bmg"
$loc = "West Europe"
$vnet = Get-AzureRmVirtualNetwork -ResourceGroupName $resource -Name $vnetName
$sub = (Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $vnet).name
foreach ($subnet in $sub){
New-AzureRmNetworkSecurityGroup -Name $subnet-nsg -ResourceGroupName $resource -Location $loc -Force
$subnetName = Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $vnet -Name $subnet
$nsg = Get-AzureRmNetworkSecurityGroup -ResourceGroupName $resource -Name "$subnet-nsg"
$subnetName.NetworkSecurityGroup = $nsg
Set-AzureRmVirtualNetwork -VirtualNetwork $vnet -Verbose
}