首页 > 解决方案 > 向 Azure NSG 规则添加多个 IP

问题描述

我有 Azure NSG。在里面我有一个名为“Blocked_IP”的规则,我需要每天使用数千个新 ip 自动更新此规则的过程(每个规则的 Azure 最大 ip 为 4k)

查看文档我有 2 个可能的命令:

$ipsarry = Get-Content .\file.txt
az network nsg rule update -g $groupName --nsg-name $NSGName -n $ruleName --source-address-prefix $ipsarry

file.txt 将保存 ips。这将起作用,只要 file.txt 保持 aprox 小于 500 ips,就好像我添加更多命令将是一个非常长的字符串并且会有异常。我无法部分添加它,因为我找不到附加数据的方法,每次调用时都会删除旧数据。

使用其他命令


$NSG = Get-AzNetworkSecurityGroup -Name 'test' -ResourceGroupName 'Testik'


$Params = @{
  'Name'                     = 'auto_farm_protection4'
  'NetworkSecurityGroup'     = $NSG
  'Protocol'                 = 'TCP'
  'Direction'                = 'Inbound'
  'Priority'                 = 500
  'SourceAddressPrefix'      = "1.1.1.1, 2.2.2.2"
  'SourcePortRange'          = '*'
  'DestinationAddressPrefix' = '*'
  'DestinationPortRange'     = 3389
  'Access'                   = 'Deny'
}

Add-AzNetworkSecurityRuleConfig @Params | Set-AzNetworkSecurityGroup

会给我同样的问题。

看起来谷歌对我帮助不大。任何帮助都会很棒。

标签: azurepowershell

解决方案


您可以使用Set-AzNetworkSecurityRuleConfig -Name <String> -NetworkSecurityGroup <PSNetworkSecurityGroup> -SourceAddressPrefix String[]. 将文本文件中的 ip 地址加载到字符串数组中(我猜你有一个分隔 ip 地址的分隔符)。最后,将其传递给SourceAddressPrefix的参数。

https://docs.microsoft.com/en-us/powershell/module/az.network/set-aznetworksecurityruleconfig?view=azps-6.6.0


推荐阅读