将带有标签的Azure资源组导出到csv [英] Export Azure Resource Groups with Tags to csv

查看:37
本文介绍了将带有标签的Azure资源组导出到csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Powershell的新手,我正在尝试制作一个脚本,以从Azure资源组中获取所有数据(包括标签)并将输出导出为CSV.

I'm kinda new to Powershell, and I'm trying to make a script that gets all the data from Azure Resource Group (including tags) and exporting the output to CSV.

据我所知,有几种方法可以做到这一点,AzureCLI,AzureRM(Powershell)和Az(Powershell).

As far as I know, there are a few ways to do it, AzureCLI, AzureRM(Powershell), and Az(Powershell).

示例1:这个脚本一线式"可以完成工作,但是它需要输入实际的标签,而不是从Azure中自动检索标签

Example 1: This "script one-liner" gets the work done, but it requires to input the actual tags, instead of retrieving it automatically from Azure

$resourceGroupList = Get-AzResourceGroup | select-Object -Property ResourceGroupName,Location,ResourceId,ProvisioningState,@{N='Enviroment (Tag)'; E={$_.Tags.Enviroment}} ,@{N='Ownership (Tag)'; E={$_.Tags.Ownership}} 
$resourceGroupList | export-csv test.csv -NoTypeInformation

我发现的另一种方法是使用AzureCLI

The other way I found is to use AzureCLI

$resourceGroupList = az group list --query "[].{ResourceGroupName:name,Location:location,ResourceType:type,provisioningState:properties,Tags:tags,ResourceGroupID:id}" -o json | convertfrom-json
$resourceGroupList | Export-Csv test.csv -NoTypeInformation

我真的很在意数组以及如何格式化数组以将其导出为CSV,如示例1所示.

I really struggle with arrays and how to format them to be exported to CSV like in the format of Example 1.

任何帮助/想法都将不胜感激!

Any help/ideas would be extremely appreciated!

谢谢!

推荐答案

这里主要要了解的是如何使用哈希表,因为这是Tags属性所包含的内容.我们需要说明的另一件事是,无论我们作为管理员如何努力,标签都是不一致的,如果我们只是为每个单独的资源组添加标签的属性,这将导致PSObjects数组中的属性不一致.因此,在开始对CSV文件的数据进行任何排序之前,我们需要所有组中唯一的标签列表.没有该标签的资源组将仅需要该属性,以便为生成的CSV文件提供一整套数据.无论如何,少说话,多代码.

The main thing to understand here is how to work with hashtables as that is what the Tags property contains. The other thing we need to account for is that tags are not consistent, no matter how hard we try as administrators, this will lead to inconsistent properties in our array of PSObjects if we just add properties for the tags from each individual Resource Group. So in advance of starting any sorting of the data for the CSV file, we need a unique list of tags across all groups. Resource Groups that don't have that tag, will need the property just so we have a complete set of data for the resulting CSV file. Anyway, less talking, more code.

# Initialise output array
$Output = @()

# Collect all the groups from the current subscription
$ResourceGroups = Get-AzResourceGroup

# Obtain a unique list of tags for these groups collectively
$UniqueTags = $ResourceGroups.Tags.GetEnumerator().Keys | Select-Object -Unique

# Loop through the resource groups
foreach ($ResourceGroup in $ResourceGroups) {
    # Create a new ordered hashtable and add the normal properties first.
    $RGHashtable = [ordered] @{}
    $RGHashtable.Add("Name",$ResourceGroup.ResourceGroupName)
    $RGHashtable.Add("Location",$ResourceGroup.Location)
    $RGHashtable.Add("Id",$ResourceGroup.ResourceId)
    $RGHashtable.Add("ProvisioningState",$ResourceGroup.ProvisioningState)

    # Loop through possible tags adding the property if there is one, adding it with a hyphen as it's value if it doesn't.
    if ($ResourceGroup.Tags.Count -ne 0) {
        $UniqueTags | Foreach-Object {
            if ($ResourceGroup.Tags[$_]) {
                $RGHashtable.Add("$_ (Tag)",$ResourceGroup.Tags[$_])
            }
            else {
                $RGHashtable.Add("$_ (Tag)","-")
            }
        }
    }
    else {
        $UniqueTags | Foreach-Object { $RGHashtable.Add("$_ (Tag)","-") }
    }

    # Update the output array, adding the ordered hashtable we have created for the ResourceGroup details.
    $Output += New-Object psobject -Property $RGHashtable
}

# Sent the final output to CSV
$Output | Export-Csv -Path test.csv -NoClobber -NoTypeInformation -Encoding UTF8 -Force

我只用类似结构的一些基本数据进行了测试,因为我目前不在工作计算机上.

I ran a test with just some basic data in a similar structure, because I'm not on my work machine currently.

$eur = "" | select ResourceGroupName,Location,Tags,ResourceId,ProvisioningState
$asia = "" | select ResourceGroupName,Location,Tags,ResourceId,ProvisioningState
$na = "" | select ResourceGroupName,Location,Tags,ResourceId,ProvisioningState
$sa = "" | select ResourceGroupName,Location,Tags,ResourceId,ProvisioningState

$eur.ResourceGroupName = "ParisDC"
$eur.Location = "westeurope"
$eur.ResourceId = 1
$eur.ProvisioningState = "Succeeded"
$tags = @{
    Computer = "FRDC01"
    IP = "10.11.10.10"
    Datacenter = "West Europe"
    CostCode = 54321
}
$eur.Tags = $tags

$asia.ResourceGroupName = "TokyoDC"
$asia.Location = "eastasia"
$asia.ResourceId = 2
$asia.ProvisioningState = "Succeeded"
$tags = @{
    Server = "TODC01"
    IP = "10.12.10.10"
    CostCode = 98765
}
$asia.Tags = $tags

$na.ResourceGroupName = "NewYorkDC"
$na.Location = "eastus"
$na.ResourceId = 3
$na.ProvisioningState = "Failed"
$tags = @{
    Computer = "USDC01"
    IP = "10.10.10.10"
    Owner = "John Smith"
    CostCode = 12345
}
$na.Tags = $tags

$sa.ResourceGroupName = "RioDC"
$sa.Location = "brazilsouth"
$sa.ResourceId = 4
$sa.ProvisioningState = "Succeeded"
$tags = @{}
$sa.Tags = $tags

$ResourceGroups += $sa,$na,$eur,$asia

如果要查看示例,只需复制并粘贴数据,然后在我提供的代码中省略 $ ResourceGroups = Get-AzResourceGroup 行.

If you want to see the sample, just copy and paste the data, and then omit the line $ResourceGroups = Get-AzResourceGroup in the code I provided.

结果输出:

Name      Location    Id ProvisioningState IP (Tag)    Computer (Tag) Owner (Tag) CostCode (Tag) Datacenter (Tag) Server (Tag)
----      --------    -- ----------------- --------    -------------- ----------- -------------- ---------------- ------------
RioDC     brazilsouth  4 Succeeded         -           -              -           -              -                -
NewYorkDC eastus       3 Failed            10.10.10.10 USDC01         John Smith  12345          -                -
ParisDC   westeurope   1 Succeeded         10.11.10.10 FRDC01         -           54321          West Europe      -
TokyoDC   eastasia     2 Succeeded         10.12.10.10 -              -           98765          -                TODC01

这篇关于将带有标签的Azure资源组导出到csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆