Search code examples
azureazure-powershellazure-rm

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


I can configure a single rule successfully in my network security group with the following code for one source IP address ($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

I would like to configure this single rule for multiple IPs, but when I providing $SourceAddressPrefix="x.x.x.x, y.y.y.y" (just as I could do in Azure Portal interactively) I got the following error:

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

Question

How can I provide multiple IPs in a single rule, just as I can do it in Azure Portal?


Solution

  • you need to give it an array of values (because it expects System.Collections.Generic.List1[System.String]):

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

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