![](/img/trans.png)
[英]How to List Azure Network Security Group from all Subscription using powershell
[英]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
}
}
}
正如您在上面的 output 中看到的那樣,它為連接到哪些 NIC 和子網的 NSG 返回一個空白值。
我也嘗試了一些類似的代碼更改
@{Name='NetworkInterfaceName';Expression={$nsg.NetworkInterfaces}},
@{Name='SubnetName';Expression={$nsg.Subnets}}
但也給出了一個空白列 output。
我正在嘗試獲取 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
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.