Adding bulk Network Security Group (NSG) rules with Azure PowerShell

Following PowerShell Script can help you to automate the NSG rules creation. In the following example, I am providing access to 20 sample Public IP addresses to my Virtual Machine’s Public IP in port 1433

$NSG = Get-AzNetworkSecurityGroup -Name zimbra01-nsg -ResourceGroupName MailSystem
foreach($rule in import-csv "input.csv")
{ 
    $NSG | Add-AzNetworkSecurityRuleConfig -Name $rule.name -Access Allow -Protocol Tcp -Direction $rule.direction -Priority $rule.priority -SourceAddressPrefix $rule.source -SourcePortRange * -DestinationAddressPrefix $rule.destination -DestinationPortRange $rule.port
}

$NSG | Set-AzNetworkSecurityGroup

Following csv file has been used as the input file

name,direction,priority,source,destination,port
public1,Inbound,1001,10.90.100.101,20.200.93.212,1433
public2,Inbound,1002,10.90.100.102,20.200.93.212,1433
public3,Inbound,1003,10.90.100.103,20.200.93.212,1433
public4,Inbound,1004,10.90.100.104,20.200.93.212,1433
public5,Inbound,1005,10.90.100.105,20.200.93.212,1433
public6,Inbound,1006,10.90.100.106,20.200.93.212,1433
public7,Inbound,1007,10.90.100.107,20.200.93.212,1433
public8,Inbound,1008,10.90.100.108,20.200.93.212,1433
public9,Inbound,1009,10.90.100.109,20.200.93.212,1433
public10,Inbound,1010,10.90.100.110,20.200.93.212,1433
public11,Inbound,1011,10.90.100.111,20.200.93.212,1433
public12,Inbound,1012,10.90.100.112,20.200.93.212,1433
public13,Inbound,1013,10.90.100.113,20.200.93.212,1433
public14,Inbound,1014,10.90.100.114,20.200.93.212,1433
public15,Inbound,1015,10.90.100.115,20.200.93.212,1433
public16,Inbound,1016,10.90.100.116,20.200.93.212,1433
public17,Inbound,1017,10.90.100.117,20.200.93.212,1433
public18,Inbound,1018,10.90.100.118,20.200.93.212,1433
public19,Inbound,1019,10.90.100.119,20.200.93.212,1433
public20,Inbound,1020,10.90.100.120,20.200.93.212,1433

Hope this script helps you to automate the NSG rules addition !

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Comments