【问题标题】:Azure (PowerShell) How to add an additional IP to multiple existent Network Security Groups (Source Address Prefix field)?Azure(PowerShell)如何向多个现有的网络安全组(源地址前缀字段)添加额外的 IP?
【发布时间】:2022-01-14 16:43:03
【问题描述】:

我需要一些帮助才能将额外的 IP (122.21.20.3/12) 添加到 Azure 中的一堆 NSG。这是为了允许额外的源地址。 我能够编写一个脚本来帮助我找到受影响的 NSG。我只需要将新 IP 添加到包含另一个类似 IP (122.21.20.2/12) 的 NSG:

$azSubs = Get-AzSubscription

foreach ( $azSub in $azSubs ) {
    Set-AzContext -Subscription $azSub | Out-Null

    $azNsgs = Get-AzNetworkSecurityGroup 
    
    foreach ( $azNsg in $azNsgs ) {
        Get-AzNetworkSecurityRuleConfig -NetworkSecurityGroup $azNsg | Where-Object { $_.SourceAddressPrefix -eq '122.21.20.2/12' } | `
            Select-Object @{label = 'NSG Name'; expression = { $azNsg.Name } }, 
                          @{label = 'Rule Name'; expression = { $_.Name } },
                          @{label = 'Source IP'; expression = { $_.SourceAddressPrefix } },
                          @{label = 'Port Range'; expression = { $_.DestinationPortRange } }, Access, Priority, Direction, `
                          @{label = 'Resource Group Name'; expression = { $azNsg.ResourceGroupName } } 
      
    }    
}

我可以获得受影响的 NSG 列表。不知道如何将其放入每个的 SourceAddressPrefix 中。 Set-AzNetworkSecurityRuleConfig 是否用于此目的?请问谁有例子吗?

非常感谢!

【问题讨论】:

    标签: azure powershell powershell-2.0 powershell-3.0 azure-powershell


    【解决方案1】:

    是的,但您需要更改您的 NSG。

    大概是这样的吧?

    $NSG = Get-AzNetworkSecurityGroup -Name 'MyNSG' -ResourceGroupName 'MyRG'
    
    $Params = @{
      'Name'                     = 'NewRule'
      'NetworkSecurityGroup'     = $NSG
      'Protocol'                 = '*'
      'Direction'                = 'Outbound'
      'Priority'                 = 200
      'SourceAddressPrefix'      = '*'
      'SourcePortRange'          = '*'
      'DestinationAddressPrefix' = '*'
      'DestinationPortRange'     = @('80', '443')
      'Access'                   = 'Deny'
    }
    
    Add-AzNetworkSecurityRuleConfig @Params | Set-AzNetworkSecurityGroup
    

    【讨论】:

      【解决方案2】:

      基于上述要求,我们创建了以下 PowerShell 脚本,它将提取所有现有的网络安全组及其各自的 NSG 规则。

      我们在下面的脚本中添加了一个条件,以仅提取我们想要的具有 ParticularIP 的 SourceAddressPrefix 的 NSG 规则,并且它将使用必需的 SourceIPAddressPrefixes 更新 NSG 规则

      这是 PowerShell 脚本:

      connect-azaccount
      
      $requiredIp=("10.x.x.x/27") ##Ip that you want to check 
      
      $ngs=Get-AzNetworkSecurityGroup ##list all Network Security Groups in the subscription
      
      foreach($ng in $ngs){
          $nsgrule=$ng.SecurityRules ##appending the nsg rules of that particular Network Security Groups
      
      foreach( $item in $nsgrule) {
          $ruleip=$item| Select-Object -Property SourceAddressPrefix,name ##pulling the sourceIPAddressPrefix of that existing NSG rule
             
      foreach( $ip in $ruleip)
          {
              if( $ip.SourceAddressPrefix -eq $requiredIp){
              
              $rec=Get-AzNetworkSecurityGroup -Name $ng.Name
      
              ## add the required IP in the "-SourceAddressPrefix" flag in the below cmdlet to update the NSG rule with the required IP address
      
              Set-AzNetworkSecurityRuleConfig -Name $ip.Name -NetworkSecurityGroup $rec -SourceAddressPrefix ($($requiredIp),"10.x.x.x/27") -Protocol Tcp -Access Allow -Direction Inbound -DestinationAddressPrefix * -SourcePortRange * -DestinationPortRange * -Priority 310
              Set-AzNetworkSecurityGroup -NetworkSecurityGroup $rec
          }
         }
      }
      }
      

      这是供参考的示例输出:

      【讨论】:

      • 它有效,非常感谢!是否可以保持“优先级”参数不变?
      • 是的,您可以使用相同的优先级,它将覆盖现有的 NSG 规则。
      • 那会很困难。有数百个 NSG,具有不同的优先级。有没有办法强制优先级使用通配符参数?任何想法?非常感谢!
      • 您是要在上述脚本中保持优先级作为条件,还是要在set-aznetworksecurityruleconfig cmdlet 中使用优先级标志?
      • 我想以某种方式保持受影响规则的优先级。我看到 Set-AzNetworkSecurityRuleConfig 的 Priority 参数是强制性的,不接受通配符。你有什么想法吗?
      【解决方案3】:

      执行此任务的完整脚本是:

      connect-azaccount
      
      $requiredIp=("10.x.x.x/27") ##Ip that you want to check 
      
      $ngs=Get-AzNetworkSecurityGroup ##list all Network Security Groups in the subscription
      
      foreach($ng in $ngs){
          $nsgrule=$ng.SecurityRules ##appending the nsg rules of that particular Network Security Groups
      
      foreach( $item in $nsgrule) {
          $ruleip=$item| Select-Object -Property SourceAddressPrefix,name ##pulling the sourceIPAddressPrefix of that existing NSG rule
             
      foreach( $ip in $ruleip)
          {
              if( $ip.SourceAddressPrefix -eq $requiredIp){
              
              $rec=Get-AzNetworkSecurityGroup -Name $ng.Name
      
              ## add the required IP in the "-SourceAddressPrefix" flag in the below cmdlet to update the NSG rule with the required IP address
      
              Set-AzNetworkSecurityRuleConfig `
                  -Name $ip.Name `
                  -NetworkSecurityGroup $rec `
                  -SourceAddressPrefix ( @($item.SourceAddressPrefix) + $newIP ) `
                  -Protocol * `
                  -Access Allow `
                  -Direction Inbound `
                  -DestinationAddressPrefix * `
                  -SourcePortRange * `
                  -DestinationPortRange * `
                  -Priority $item.Priority
              Set-AzNetworkSecurityGroup -NetworkSecurityGroup $rec
          }
         }
      }
      }
      

      【讨论】:

        猜你喜欢
        • 2020-10-20
        • 1970-01-01
        • 2022-01-15
        • 2012-12-11
        • 2023-02-09
        • 2019-04-18
        • 2015-11-02
        • 2017-04-01
        • 2023-03-21
        相关资源
        最近更新 更多