简体   繁体   English

如何列出 Azure 网络安全组

[英]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.我创建了一个 Powershell 脚本,该脚本在所有订阅中提供所有可用的 Azure 网络安全组。 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:它提供 output 如下: 输出-CSV

as you can see in the above output it is returning a blank value for NSG attached to which NICs and Subnets.正如您在上面的 output 中看到的那样,它为连接到哪些 NIC 和子网的 NSG 返回一个空白值。

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.但也给出了一个空白列 output。

I am trying to get NICs and subnet to which NSGs are linked.我正在尝试获取 NSG 链接到的 NIC 和子网。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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