我对 Powershell 还不太熟悉,我正在尝试编写一个脚本来从 Azure 资源组获取所有数据(包括标签)并将输出导出到 CSV。
据我所知,有几种方法可以做到这一点,AzureCLI、AzureRM(Powershell) 和 Az(Powershell)。
示例 1: 这个“单行脚本”可以完成工作,但它需要输入实际标签,而不是从 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
$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
我真的很难理解数组以及如何将它们格式化为以示例 1 的格式导出为 CSV。
任何帮助/想法将不胜感激!
谢谢!
最佳答案
这里要理解的主要内容是如何使用哈希表,因为这就是 Tags 属性所包含的内容。我们需要考虑的另一件事是标签不一致,无论我们作为管理员多么努力,如果我们只是为每个单独的资源组中的标签添加属性,这都会导致 PSObject 数组中的属性不一致。因此,在开始对 CSV 文件的数据进行任何排序之前,我们需要一个跨所有组的唯一标签列表。没有该标签的资源组将需要该属性,以便我们拥有生成的 CSV 文件的完整数据集。不管怎样,少说话,多写代码。
# 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
# Use Sort-Object as per - https://github.com/PowerShell/PowerShell/issues/12059
$UniqueTags = $ResourceGroups.Tags.GetEnumerator().Keys | Sort-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
我仅使用类似结构中的一些基本数据进行了测试,因为我当前不在我的工作计算机上。
$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
行。
结果输出:
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 - 将带有标签的 Azure 资源组导出到 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62098358/