【问题标题】:Export Azure Network Security Group Rules to CSV using powershell使用 powershell 将 Azure 网络安全组规则导出为 CSV
【发布时间】:2016-03-16 12:09:05
【问题描述】:

我目前正在完成 Azure 网络安全组的设置,并试图找到更好的方法来维护我们的规则。我有一个用于 azure powershell 的脚本,可以通过 CSV 创建安全规则,但想导出。运行以下命令时

Get-AzureNetworkSecurityGroup -Name "name" -Detailed | export-Csv c:/file.csv

我得到了文件,但它没有向我提供 csv 的详细信息。它的行为就好像它忽略了 -Detailed 命令。有人有答案吗?

【问题讨论】:

    标签: powershell azure azure-powershell azure-virtual-network


    【解决方案1】:

    您可以运行以下脚本以在一个 CSV 中获取所有 NSG 及其所有规则:

     $azNsgs = Get-AzNetworkSecurityGroup 
    
    foreach ( $azNsg in $azNsgs ) {
        # custom rules
        Get-AzNetworkSecurityRuleConfig -NetworkSecurityGroup $azNsg | `
            Select-Object @{label = 'NSG Name'; expression = { $azNsg.Name } }, `
            @{label = 'NSG Location'; expression = { $azNsg.Location } }, `
            @{label = 'Rule Name'; expression = { $_.Name } }, `
            @{label = 'Source'; expression = { $_.SourceAddressPrefix } }, `
            @{label = 'Source Application Security Group'; expression = { $_.SourceApplicationSecurityGroups.id.Split('/')[-1] } },
            @{label = 'Source Port Range'; expression = { $_.SourcePortRange } }, Access, Priority, Direction, `
            @{label = 'Destination'; expression = { $_.DestinationAddressPrefix } }, `
            @{label = 'Destination Application Security Group'; expression = { $_.DestinationApplicationSecurityGroups.id.Split('/')[-1] } }, `
            @{label = 'Destination Port Range'; expression = { $_.DestinationPortRange } }, `
            @{label = 'Resource Group Name'; expression = { $azNsg.ResourceGroupName } } | `
            Export-Csv -Path "$($home)\tf\nsg-rules.csv" -NoTypeInformation -Append -force
        
        #  default rules
        Get-AzNetworkSecurityRuleConfig -NetworkSecurityGroup $azNsg -Defaultrules | `
            Select-Object @{label = 'NSG Name'; expression = { $azNsg.Name } }, `
            @{label = 'NSG Location'; expression = { $azNsg.Location } }, `
            @{label = 'Rule Name'; expression = { $_.Name } }, `
            @{label = 'Source'; expression = { $_.SourceAddressPrefix } }, `
            @{label = 'Source Port Range'; expression = { $_.SourcePortRange } }, Access, Priority, Direction, `
            @{label = 'Destination'; expression = { $_.DestinationAddressPrefix } }, `
            @{label = 'Destination Port Range'; expression = { $_.DestinationPortRange } }, `
            @{label = 'Resource Group Name'; expression = { $azNsg.ResourceGroupName } } | `
            Export-Csv -Path "$($home)\tf\nsg-rules.csv" -NoTypeInformation -Append -force
           
      
    }    
    

    【讨论】:

      【解决方案2】:

      试试这个:

      (Get-AzureNetworkSecurityGroup -Name "name" -Detailed).Rules | Export-csv -path "C:\nsgfile.csv"
      

      【讨论】:

      • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
      • 请在回答中添加一些解释以提高您的回答质量
      【解决方案3】:

      如果您在子网级别而不是在 VM 级别应用 NSG,这肯定会对您有所帮助。首先使用-找出nsg名称-

      $nsgName = (Get-AzureNetworkSecurityGroupForSubnet -VirtualNetworkName "MYNetwork" -SubnetName "MySubnet").Name
      

      现在使用 nsg 名称查找详细的 NGS 详细信息-

      Get-AzureNetworkSecurityGroup -Name $nsgName -Detailed | Export-csv -path "C:\nsgfile.csv"
      

      上述命令在控制台上的输出-

      [更新答案]

      【讨论】:

      • 这只是导出子网而不导出实际规则。标题只是规则名称位置标签。
      • 我相信你 nsg 没有到位,这就是它没有拉出正确输出的原因。首先检查 nsg 是否到位并正确应用。 Get-AzureNetworkSecurityGroupForSubnet -VirtualNetworkName "MYNetwork" -SubnetName "MySubnet"
      • 这与使用我上面使用的相同脚本不一样吗?您只是手动创建了一个 NSG 组。
      • 我不是在这里创建一个新的 nsg。我只是先提取 nsg 名称,然后再提取详细信息。如果 NSG 本身不存在,它将为您提取详细信息,否则将引发错误。
      • 你能重读我原来的问题吗?这就是我遇到麻烦的确切命令。
      猜你喜欢
      • 2021-06-10
      • 1970-01-01
      • 2022-11-04
      • 2015-08-28
      • 2020-04-04
      • 1970-01-01
      • 2021-10-10
      • 2021-04-25
      • 1970-01-01
      相关资源
      最近更新 更多