简体   繁体   中英

How to List Azure Network Security Group

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        
        }
    }
}

It provides output like this: 输出-CSV

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.

I solved the above question. Please find the script for the same.

############# 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.NetworkInterfaces.Id.Split('/')[-1]}},
                @{Name='SubnetName';Expression={$nsg.Subnets.Id.Split('/')[-1]}} |
                    Export-Csv "C:\Users\admin-vishal.singh\Desktop\Test\nsg\NSG-C100.csv" -NoTypeInformation -Encoding ASCII -Append        
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM