如何列出 Azure 网络安全组

How to List Azure Network Security Group

我创建了一个 Powershell 脚本,它提供了所有订阅中所有可用的 Azure 网络安全组。 脚本是这样的:

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

它提供如下输出:

如您在上面的输出中所见,它为连接到 NIC 和子网的 NSG 返回空白值。

我也尝试过对代码进行一些类似的更改

@{Name='NetworkInterfaceName';Expression={$nsg.NetworkInterfaces}},
@{Name='SubnetName';Expression={$nsg.Subnets}} 

但也给出一个空白列作为输出。

我正在尝试获取 NSG 链接到的 NIC 和子网。

我解决了上面的问题。 请找到相同的脚本。

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