Azure - NSG Rules to CSV

The Following PowerShell can be used to extract NSG rules to CSV:

Import-Module AzureRM

$nsgs = Get-AzureRmNetworkSecurityGroup

$exportPath = 'C:\temp'

 

Foreach ($nsg in $nsgs) {

    New-Item -ItemType file -Path "$exportPath\$($nsg.Name).csv" -Force

    $nsgRules = $nsg.SecurityRules

    foreach ($nsgRule in $nsgRules) {

        $nsgRule | Select-Object Name,Description,Priority,@{Name=’SourceAddressPrefix’;Expression={[string]::join(“,”, ($_.SourceAddressPrefix))}},@{Name=’SourcePortRange’;Expression={[string]::join(“,”, ($_.SourcePortRange))}},@{Name=’DestinationAddressPrefix’;Expression={[string]::join(“,”, ($_.DestinationAddressPrefix))}},@{Name=’DestinationPortRange’;Expression={[string]::join(“,”, ($_.DestinationPortRange))}},Protocol,Access,Direction `

        | Export-Csv "$exportPath\$($nsg.Name).csv" -NoTypeInformation -Encoding ASCII -Append

    }

}

Comments