Categories: Uncategorized

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
king

Recent Posts

Restore VM from Recovery Service Vault – PowerShell

# Mention your VM here $VMName = "SubhenduTestVM" # Mention your Stage Storage here $stagestorageaccountname…

4 years ago

Power Of Powershell 2

Hope you are doing great . Current Time is very critical for us . I…

4 years ago

Power of Powershell

Being a part of Microsoft Education I have explored powershell long time back . But…

4 years ago

New Azure Exam is Ready to Launch

Sorry !! But Yes .  Microsoft is about to release new exam in Azure Segment…

4 years ago

DFSR to Azure File Sync | DFS cross domain Migration | Azure File Sync Agent issue and Solution

Migration is always a part of our life and part of this fantastic IT world…

4 years ago

Powershell Script to create Snapshot from VM and move to storage account

I believe you have enjoyed my last script related to VM snapshot creation . Sometime…

4 years ago