如何使用PowerShell列出所有订阅的Azure网络安全组?

3
我正在尝试创建一个PowerShell脚本,以列出所有订阅的Azure网络安全组及其规则,并将其导出为CSV文件。
以下是我的代码,它列出了所有NSG规则名称、描述、优先级、源地址前缀、源端口范围、目标地址前缀、目标端口范围、协议、访问和方向。
############# 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 @{n='SubscriptionName';e={$sub.Name}},
                @{n='ResourceGroupName';e={$nsg.ResourceGroupName}},
                @{n='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 |
                    Export-Csv "C:\Users\admin-vishal.singh\Desktop\Test\nsg\NsgRules.csv" -NoTypeInformation -Encoding ASCII -Append        
        }
    }
}

上述脚本的输出如下图所示: enter image description here 我还尝试在$nsgRule | Select-Object下调用Resourcegroup、SubscriptionName对象,但是它给出了带有Resourcegroup、subscriptionName标题的空列。
我想要的输出结果如下图所示: enter image description here 我不知道需要在哪个for循环中进行更改才能得到上述输出。基本上,我正在尝试列出所有Azure NSG和来自所有订阅的规则,以及它们各自的ResourcegroupName和subscriptionName。
2个回答

4
你想要返回的额外属性属于不同的对象,而不是 $nsgRule。你仍然可以通过使用Select-Object的计算属性来检索这些属性。
$subs = Get-AzureRmSubscription

foreach ($sub in $subs) {
    Select-AzureRmSubscription -SubscriptionId $sub.Id
    $nsgs = Get-AzureRmNetworkSecurityGroup

    Foreach ($nsg in $nsgs) {
        $nsgRules = $nsg.SecurityRules

        foreach ($nsgRule in $nsgRules) {
            $nsgRule | Select-Object @{n='SubscriptionName';e={$sub.Name}},
                @{n='ResourceGroupName';e={$nsg.ResourceGroupName}},
                @{n='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 |
                    Export-Csv "C:\Vishal\NsgRules.csv" -NoTypeInformation -Encoding ASCII -Append        
        }
    }
}

$nsg包含ResourceGroupNameName(网络安全组名称)。 $sub包含订阅名称作为Name


我尝试了这个,但它输出为空的CSV文件。 - undefined
我刚刚在Azure云Shell中运行了它,并输出了所有内容。不过,我使用了Azure云存储作为我的输出位置。 - undefined
我尝试在Azure云Shell上运行,但出现了错误。 ./Azurecli-nsg.ps1 "nsg list" 出现以下错误 admin-vishal_singh@Azure:~/clouddrive$ ./Azurecli-nsg.ps1 "nsg list" ./Azurecli-nsg.ps1: 第2行:=: 找不到命令 ./Azurecli-nsg.ps1: 第3行:$'\r': 找不到命令 ./Azurecli-nsg.ps1: 第4行:附近的语法错误 $sub' '/Azurecli-nsg.ps1: 第4行:foreach ($sub in $subs) { - undefined
我正在运行你发布的相同代码,并添加了额外的属性来选择对象。所以如果我的代码有问题,那么你的代码应该也有同样的问题。 - undefined
我修复了你原始脚本中的所有弯引号。也许它们导致了问题。 - undefined
我觉得我的脚本有空格的问题,我尝试整理了一下代码,现在它可以正常工作了。 谢谢你的帮助。 - undefined

1
不确定这是否有点帮助...我正在做类似的事情,并使用az cli(它可以直接转换为powershell)。
我采取了以下方法:
#get all the subs in a list
$subs_list = az account list --query [].[id] -o tsv

#loop through list of subs
foreach($sub in $subs_list)
{
    az account set --subscription $sub # change the active sub
    $rg_list = az group list | ConvertFrom-json # retrieve a list of resource groups for that sub
    foreach($rg in $rg_list) # loop through the list of resource groups
    {
        $nsg_list = az network nsg list --resource-group $rg.name | ConvertFrom-json # get the list of nsg for a resource group
        foreach($nsg in $nsg_list) # loop through the list of nsg
        {
            $nsg_rules = az network nsg rule list --nsg $nsg.name --resource-group $rg.name -o table # get a list of rules for each nsg
            $nsg_rules # print the rules to screen
        }
        

    }
}

你需要弄清楚如何对其进行格式化,但是现在你已经通过$sub获得了订阅对象,并且通过$rg.name或$rg.id获得了资源组...

希望这能有所帮助,我也是新手,在这里回答的第一个问题 :)


你是在混用az cli和Powershell吗? - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接