Jeff
Jeff

Reputation: 21

Export Azure Network Security Group Rules to CSV using powershell

I'm currently finishing up setting up our Azure network Security Groups and trying to find better ways to maintain our rules. I Have a script for azure powershell to create the security rules via CSV but wanted to export. When running the following command

Get-AzureNetworkSecurityGroup -Name "name" -Detailed | export-Csv c:/file.csv

I get the file but it doesn't give me the details to csv. It acts as if it ignores the -Detailed command. Anyone have the answer?

Upvotes: 2

Views: 12324

Answers (3)

Ankit Kotnala
Ankit Kotnala

Reputation: 83

You can run below script to get all of NSGs and all of their rules in one single CSV :

 $azNsgs = Get-AzNetworkSecurityGroup 

foreach ( $azNsg in $azNsgs ) {
    # custom rules
    Get-AzNetworkSecurityRuleConfig -NetworkSecurityGroup $azNsg | `
        Select-Object @{label = 'NSG Name'; expression = { $azNsg.Name } }, `
        @{label = 'NSG Location'; expression = { $azNsg.Location } }, `
        @{label = 'Rule Name'; expression = { $_.Name } }, `
        @{label = 'Source'; expression = { $_.SourceAddressPrefix } }, `
        @{label = 'Source Application Security Group'; expression = { $_.SourceApplicationSecurityGroups.id.Split('/')[-1] } },
        @{label = 'Source Port Range'; expression = { $_.SourcePortRange } }, Access, Priority, Direction, `
        @{label = 'Destination'; expression = { $_.DestinationAddressPrefix } }, `
        @{label = 'Destination Application Security Group'; expression = { $_.DestinationApplicationSecurityGroups.id.Split('/')[-1] } }, `
        @{label = 'Destination Port Range'; expression = { $_.DestinationPortRange } }, `
        @{label = 'Resource Group Name'; expression = { $azNsg.ResourceGroupName } } | `
        Export-Csv -Path "$($home)\tf\nsg-rules.csv" -NoTypeInformation -Append -force
    
    #  default rules
    Get-AzNetworkSecurityRuleConfig -NetworkSecurityGroup $azNsg -Defaultrules | `
        Select-Object @{label = 'NSG Name'; expression = { $azNsg.Name } }, `
        @{label = 'NSG Location'; expression = { $azNsg.Location } }, `
        @{label = 'Rule Name'; expression = { $_.Name } }, `
        @{label = 'Source'; expression = { $_.SourceAddressPrefix } }, `
        @{label = 'Source Port Range'; expression = { $_.SourcePortRange } }, Access, Priority, Direction, `
        @{label = 'Destination'; expression = { $_.DestinationAddressPrefix } }, `
        @{label = 'Destination Port Range'; expression = { $_.DestinationPortRange } }, `
        @{label = 'Resource Group Name'; expression = { $azNsg.ResourceGroupName } } | `
        Export-Csv -Path "$($home)\tf\nsg-rules.csv" -NoTypeInformation -Append -force
       
  
}    

Upvotes: 1

atrw sr
atrw sr

Reputation: 151

Try this :

(Get-AzureNetworkSecurityGroup -Name "name" -Detailed).Rules | Export-csv -path "C:\nsgfile.csv"

Upvotes: 1

Aatif Akhter
Aatif Akhter

Reputation: 2206

If you are applying NSG on subnet level and NOT on VM level this will surely help you. Firstly find out the nsg name using-

$nsgName = (Get-AzureNetworkSecurityGroupForSubnet -VirtualNetworkName "MYNetwork" -SubnetName "MySubnet").Name

Now use the nsg name to find out the detailed NGS details-

Get-AzureNetworkSecurityGroup -Name $nsgName -Detailed | Export-csv -path "C:\nsgfile.csv"

Output of above command on console- enter image description here

[Updated answer]

Upvotes: 1

Related Questions