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

How can I provide multiple IPs in a single rule using AzureRm PowerShell?

我可以使用以下代码为一个源 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 门户中交互式做的那样)我收到以下错误:

"...has invalid Address prefix. Value provided: x.x.x.x, y.y.y.y"

问题

如何在单个规则中提供多个 IP,就像在 Azure 门户中一样?

你需要给它一个值数组(因为它需要 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

你可以使用这个 $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