Create Complex NSG – PowerShell
Creating NSG in azure is easy unless you want to create multiple NSG with multiple inbound and outbound security rules. Here is a simple way to create that.
First open Microsoft Excel and create a .CSV file by following this format: Please do not change the format. Then save the file in CSV format.
Once you are raedy with the CSV file run bellow command. Don’t forget to login and select right subscription
#Mentioned the CSV path here $nsgCsv = Import-Csv "C:\Users\BaselineNSG.csv" $nsgName = 'Subhendu-NSG' $resourceGroup = 'TestRG' $RGLocation = (Get-AzureRmResourceGroup -name $resourceGroup).Location ####################################################################### $rulesArray = @() #create rule config foreach ($rule in $nsgCsv) { $nsgRule = New-AzureRmNetworkSecurityRuleConfig ` -Name $rule.Name ` -Description $rule.Description ` -Protocol $rule.Protocol ` -SourcePortRange ($rule.SourcePortRange -split ',') ` -DestinationPortRange ($rule.DestinationPortRange -split ',') ` -SourceAddressPrefix ($rule.SourceAddressPrefix -split ',') ` -DestinationAddressPrefix ($rule.DestinationAddressPrefix -split ',') ` -Access $rule.Access ` -Priority $rule.Priority ` -Direction $rule.Direction $rulesArray += $nsgRule } #create nsg New-AzureRmNetworkSecurityGroup -Name $nsgNames -ResourceGroupName $resourceGroup -Location $RGLocation -SecurityRules $rulesArray