I have created a Powershell script that provides all the available Azure network security groups in all the subscriptions. The script is like that:
############# List All Azure Network Security Groups #############
$subs = Get-AzSubscription
foreach ($sub in $subs) {
Select-AzSubscription -SubscriptionId $sub.Id
$nsgs = Get-AzNetworkSecurityGroup
Foreach ($nsg in $nsgs) {
$nsgRules = $nsg.SecurityRules
foreach ($nsgRule in $nsgRules) {
$nsgRule | Select-Object @{Name='SubscriptionName';Expression={$sub.Name}},
@{Name='ResourceGroupName';Expression={$nsg.ResourceGroupName}},
@{Name='NetworkSecurityGroupName';e={$nsg.Name}},
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,
@{Name='NetworkInterfaceName';Expression={$nsg.NetworkInterfacesText}},
@{Name='SubnetName';Expression={$nsg.SubnetsText}} |
Export-Csv "C:\Users\admin-vishal.singh\Desktop\Test\nsg\NSG-C10.csv" -NoTypeInformation -Encoding ASCII -Append
}
}
}
as you can see in the above output it is returning a blank value for NSG attached to which NICs and Subnets.
I also tried some changes in code like that
@{Name='NetworkInterfaceName';Expression={$nsg.NetworkInterfaces}},
@{Name='SubnetName';Expression={$nsg.Subnets}}
but also gives a blank column as output.
I am trying to get NICs and subnet to which NSGs are linked.