如何使用AzureRm PowerShell在单个规则中提供多个IP?

时间:2019-01-30 09:46:53

标签: azure azure-powershell azure-rm

我可以使用以下代码为一个源IP地址($SourceAddressPrefix="x.x.x.x")在网络安全组中成功配置单个规则:

Set-AzureRmNetworkSecurityRuleConfig  
    -NetworkSecurityGroup $nsg 
    -Name $name 
    -Direction Inbound  
    -Priority $priority 
    -Access Allow  
    -SourceAddressPrefix $sourcePrefix 
    -SourcePortRange *  
    -DestinationAddressPrefix * 
    -DestinationPortRange $destinationPortRange 
    -Protocol TCP 
 | Set-AzureRmNetworkSecurityGroup

我想为多个IP配置单个规则,但是当我提供$SourceAddressPrefix="x.x.x.x, y.y.y.y"时(就像我可以在Azure Portal中进行交互一样),出现以下错误:

  

“ ...具有无效的地址前缀。提供的值:x.x.x.x,y.y.y.y”

问题

如何像在Azure Portal中一样在单个规则中提供多个IP?

2 个答案:

答案 0 :(得分:1)

您需要为其提供一个值数组(因为它期望System.Collections.Generic.List1[System.String]):

@("x.x.x.x", "y.y.y.y")

https://docs.microsoft.com/en-us/powershell/module/azurerm.network/set-azurermnetworksecurityruleconfig?view=azurermps-6.13.0

答案 1 :(得分:1)

您可以使用此$sourcePrefix = "x.x.x.x","y.y.y.y"。这对我有效。

$nsg = Get-AzureRmNetworkSecurityGroup -ResourceGroupName "xxx" -Name "xxx"
$name = "port_1433"
$priority = 600
$sourcePrefix = "1.1.1.1","2.2.2.2"
$destinationPortRange ="1433"
Set-AzureRmNetworkSecurityRuleConfig -NetworkSecurityGroup $nsg  -Name $name `
    -Direction Inbound  `
    -Priority $priority `
    -Access Allow  `
    -SourceAddressPrefix $sourcePrefix `
    -SourcePortRange *  `
    -DestinationAddressPrefix * `
    -DestinationPortRange $destinationPortRange `
    -Protocol TCP 
$nsg | Set-AzureRmNetworkSecurityGroup

enter image description here