我是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
我真的很在意数组以及如何格式化数组以将其导出为CSV,如示例1的格式。
任何帮助/想法都将不胜感激!
谢谢!
答案 0 :(得分:4)
这里主要要了解的是如何使用哈希表,因为这是Tag属性所包含的内容。我们需要说明的另一件事是,无论我们以多大的努力尝试,标签都是不一致的,如果我们只是为每个单独的资源组添加标签的属性,这将导致PSObjects数组中的属性不一致。因此,在开始对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
$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
我只用类似结构的一些基本数据进行了测试,因为我目前不在工作计算机上。
$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
答案 1 :(得分:1)
我已经稍微更新了您的脚本并添加了其他功能。
Login-AzAccount
$Subscription = Get-AzSubscription | Out-GridView -Title 'Select subscription' -OutputMode 'Multiple'
# Initialise output array
$Output = @()
if($Subscription){
foreach ($item in $Subscription)
{
$item | Select-AzSubscription
# Collect all the resources or resource groups (comment one of below)
#$Resource = Get-AzResource
$Resource = Get-AzResourceGroup
# Obtain a unique list of tags for these groups collectively
$UniqueTags = $Resource.Tags.GetEnumerator().Keys | Get-Unique �AsString | Sort-Object | Select-Object -Unique | Where-Object {$_ -notlike "hidden-*" }
# Loop through the resource groups
foreach ($ResourceGroup in $Resource) {
# Create a new ordered hashtable and add the normal properties first.
$RGHashtable = New-Object System.Collections.Specialized.OrderedDictionary
$RGHashtable.Add("Name",$ResourceGroup.ResourceGroupName)
$RGHashtable.Add("Location",$ResourceGroup.Location)
$RGHashtable.Add("Id",$ResourceGroup.ResourceId)
$RGHashtable.Add("ResourceType",$ResourceGroup.ResourceType)
# 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 c:\temp\1a.csv -append -NoClobber -NoTypeInformation -Encoding UTF8 -Force
}
}
$Output | Out-GridView