Connect to Azure using the command below and authenticate with valid credentials.
Connect-AzAccount
After establishing Azure connectivity, execute the command below to export the NSG rule.
$nsgName = "NSG name"
$resourceGroupName = "resource group name"
# Retrieve NSG rules and flatten the lists for Source and Destination prefixes
(Get-AzNetworkSecurityGroup -Name $nsgName -ResourceGroupName $resourceGroupName).SecurityRules |
Select-Object Name, Direction, Priority, Access,
@{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 |
Export-Csv -Path "C:\nsg_rule.csv" -NoTypeInformation