1

我想根据不断变化的 IP 地址列表一次更新多个 NSG 规则。例如,我想更新名为 ftp、ssh、https(和其他几个)的规则。这是我到目前为止所拥有的:

$ips = @("10.1.1.2", "10.1.1.3", "192.168.0.0/16")
$nsgName = "Dev1-nsg"
$resourceGroupName = "myResourceGrp1"
$nsg = Get-AzNetworkSecurityGroup -Name $nsgName -ResourceGroupName $resourceGroupName
Set-AzNetworkSecurityRuleConfig -Name "ftp" -NetworkSecurityGroup $nsg -SourceAddressPrefix $ips
Set-AzNetworkSecurityRuleConfig -Name "ssh" -NetworkSecurityGroup $nsg -SourceAddressPrefix $ips
Set-AzNetworkSecurityRuleConfig -Name "https" -NetworkSecurityGroup $nsg -SourceAddressPrefix $ips
Get-AzNetworkSecurityRuleConfig -Name "ftp" -NetworkSecurityGroup $nsg
Get-AzNetworkSecurityRuleConfig -Name "ssh" -NetworkSecurityGroup $nsg
Get-AzNetworkSecurityRuleConfig -Name "https" -NetworkSecurityGroup $nsg

我的问题:

  1. 当我在 Powershell 中运行上述内容时,一切似乎都很好,但是当我转到 Azure nsg 门户并刷新时,规则似乎没有改变
  2. 如果我要更新许多规则(比如 15 条),那么循环遍历规则名称列表以使用 IP 列表更新每个规则的最佳方法是什么?
4

1 回答 1

3
  1. 我们无法在 Azure 中更新 NSG 规则,但需要修改本地 PowerShell 脚本,然后将更改推送到 Azure,请参阅此答案

  2. 要遍历规则名称列表以更新每个 IP 列表,您可以这样做。

     $ips = @("10.1.1.2", "10.1.1.3", "192.168.0.0/16")
     $nsgName = "ubun-a-nsg"
     $resourceGroupName = "nancy"
     $rule_names = @("NRMS-Rule-103","NRMS-Rule-104","NRMS-Rule-105")
    
     foreach($rule_name in $rule_names)
     {
    
     $nsg = Get-AzNetworkSecurityGroup -Name $nsgName -ResourceGroupName $resourceGroupName
     $rule= $nsg | Get-AzNetworkSecurityRuleConfig -Name $rule_name  
    
     Set-AzNetworkSecurityRuleConfig -NetworkSecurityGroup $nsg `
         -Name $rule_name `
         -Access $rule.Access `
         -Protocol $rule.Protocol `
         -Direction $rule.Direction `
         -Priority $rule.Priority `
         -SourceAddressPrefix $ips `
         -SourcePortRange $rule.SourcePortRange `
         -DestinationAddressPrefix $rule.DestinationAddressPrefix `
         -DestinationPortRange $rule.DestinationPortRange `
         -Description $rule.Description
    
     $nsg | Set-AzNetworkSecurityGroup
    
     }
    

测试结果:

在此处输入图像描述

于 2020-10-28T08:48:31.987 回答