使用 powershell 将多个 CIDR 添加到 SourceAddressPrefix 到 Azure NSG 规则

Dan*_*örk 2 powershell azure azure-nsg

使用 powershell 在 Azure 中创建 NSG 规则时,我尝试将多个 CIDR 添加到 SourceAddressPrefix。我在运行 Set-AzNetworkSecurityGroup 时收到错误消息,因此它在使用 New-AzNetworkSecurityRuleConfig 创建规则时接受该值。有人知道如何解决吗?注意:是的,我知道我可以创建多个规则,但我想尽可能避免这种情况。

使用门户可以正常工作,但使用 Powershell 时会收到错误消息。

尝试1:

 $rule3 = New-AzNetworkSecurityRuleConfig -Name "In-SandNet-Vnet-Any-Any" `
-Access Allow -Protocol *  -Direction Inbound -Priority 1000 -SourceAddressPrefix {"10.0.0.0/8","192.168.0.0/16"} `
 -SourcePortRange * -DestinationAddressPrefix VirtualNetwork  -DestinationPortRange * 

$nsg.SecurityRules.Add($rule3)

Set-AzNetworkSecurityGroup -NetworkSecurityGroup $nsg
Run Code Online (Sandbox Code Playgroud)

错误信息:

xxx has invalid Address prefix. Value provided: "10.0.0.0/8","192.168.0.0/16"
Run Code Online (Sandbox Code Playgroud)

尝试2:

$rule3 = New-AzNetworkSecurityRuleConfig -Name "In-SandNet-Vnet-Any-Any" `
    -Access Allow -Protocol *  -Direction Inbound -Priority 1000 -SourceAddressPrefix "10.0.0.0/8,192.168.0.0/16" `
     -SourcePortRange * -DestinationAddressPrefix VirtualNetwork  -DestinationPortRange * 

$nsg.SecurityRules.Add($rule3)


Set-AzNetworkSecurityGroup -NetworkSecurityGroup $nsg
Run Code Online (Sandbox Code Playgroud)

错误信息(同样的错误信息):

xxx has invalid Address prefix. Value provided: "10.0.0.0/8","192.168.0.0/16"
Run Code Online (Sandbox Code Playgroud)

Yog*_*ogi 5

New-AzNetworkSecurityRuleConfig命令需要一个String[]forSourceAddressPrefix参数。参考

所以以下应该有效:

@("10.0.0.0/8", "192.168.0.0/16")

  • 我很接近但没有雪茄给我..谢谢。这解决了它。 (2认同)