1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| param($rgName) if($rgName -eq $null) { $rgName = read-host -Prompt "please enter a rgName" } $nsgs = Get-AzureRmNetworkSecurityGroup -ResourceGroupName $rgName $exportPath = '.'
Foreach ($nsg in $nsgs) { New-Item -ItemType file -Path "$exportPath\$($nsg.Name).csv" -Force
$nsgRules = $nsg.SecurityRules | Sort-Object @{ e = 'Direction'; a = $true }, @{ e = 'Priority'; a = $true } foreach ($nsgRule in $nsgRules) { $nsgRule | Select-Object Name,Direction,Access,Priority,Protocol,@{Name=’SourceAddressPrefix’;Expression={[string]::join(“ ”, ($_.SourceAddressPrefix))}},@{Name=’SourcePortRange’;Expression={[string]::join(“ ”, ($_.SourcePortRange))}},@{Name=’DestinationAddressPrefix’;Expression={[string]::join(“ ”, ($_.DestinationAddressPrefix))}},@{Name=’DestinationPortRange’;Expression={[string]::join(“ ”, ($_.DestinationPortRange))}},Description ` | Export-Csv "$exportPath\$($nsg.Name).csv" -NoTypeInformation -Encoding UTF-8 -Append } }
|