递归列出 Azure 资源组中的所有资源标签
Recursively list all resource tags within Azure Resource Groups
我们有大量的 Azure 订阅,目前 运行 有数百个。
我希望生成一个报告(最好使用 Azure Powershell 或 Azure CLI)以递归地提取分配给每个资源组中每个资源的所有标签的列表,用于 40-50 个订阅。
目前,我可以列出在资源组级别分配的所有标签,但我根本找不到一种方法来列出分配给每个资源组中各个资源的标签。我要提取此报告的订阅和资源组列表保存在一个 CSV 文件中,该文件包含两列,分别显示订阅名称和资源组。
任何关于如何实现上述目标的提示都将是非常棒的并且非常感谢。
没有详细的代码,但这里的想法。
1.You 应该写一个循环,在循环中,每次使用此 cmdlet 更改 subscription
:
Set-AzContext -Subscription $subscription_name
.
2.Then 使用此 cmdlet 获取指定 subscription
中的所有 resource group
:
$resource_groups = Get-AzResourceGroup
3.Then 编写一个嵌套循环(每个 resource group
循环),在这个嵌套循环中,使用此 cmdlet 获取 resource group
中的所有 azure resources
:
foreach($rg in $resource_groups){
$azure_resources = Get-AzResource -ResourceGroupName $rg.ResourceGroupName
}
4.Write 是步骤 3 中的另一个嵌套循环,此循环用于遍历指定 resource group
内的所有 azure resources
。然后使用下面的代码为 resource group
:
中的每个 azure resource
获取标签
foreach($r in $azure_resources){
#the following code can get all the tags for one resource
$r.tags
}
基于伊万杨的逻辑。我已经构建了 PowerShell 脚本;
#------------声明变量-------------------------------- ---#
$bnsSubscription = Get-AzSubscription
$day = Get-Date -Format " ddMMMyyyy"
$tagPath = "C:\mytempfolder\"+"$day-Tag-Details.csv"
$tagFolderPath = "C:\mytempfolder\"
#------------声明变量-------------------------------- ---#
function Get-ResourceTag {
foreach ($subs in $bnsSubscription) {
Select-AzSubscription -SubscriptionName $subs.Name | Out-Null
Write-Host 'The selected Subscription is' ($subs).Name
New-Item -ItemType file -Path "$tagFolderPath$($subs.Name).csv" -Force
$resource_groups = Get-AzResourceGroup
$resource_groups_details = Get-AzResourceGroup | Sort-Location ResourceGroupName | Format-Table -GroupBy Location ResourceGroupName,ProvisioningState,Tags
Write-Host 'The selected Resource Group is' ($resource_groups).Name 'and the tag information as follows'
#$resource_groups_details
$resource_groups | Select-Object ResourceGroupName,Tags | Export-CSV -Path "$tagFolderPath$($subs.Name).csv" -Append
$OutputFile = @()
foreach($rg in $resource_groups){
$azure_resources = Get-AzResource -ResourceGroupName $rg.ResourceGroupName
$TestTags = $Resource.Tags.GetEnumerator()
foreach($r in $azure_resources){
Write-Host 'The selected resource is' ($r).Name 'and the information as follows'
$RGHT = New-Object "System.Collections.Generic.List[System.Object]"
$RGHT.Add("RGName",$r.ResourceGroupName)
$RGHT.Add("ResourceName",$r.name)
$RGHT.Add("Location",$r.Location)
$RGHT.Add("Id",$r.ResourceId)
$RGHT.Add("ResourceType",$r.ResourceType)
$RGHT.Add("ResourceTags",$r.Tags)
$OutputFile += New-Object psobject -Property $RGHT
$OutputFile | Export-Csv -Path "C:\mytempfolder\test22.csv" -append -NoClobber -NoTypeInformation -Encoding UTF8 -Force
}
}
}
}
#--------调用函数-------------------------------- ---#
Get-ResourceTag
我们有大量的 Azure 订阅,目前 运行 有数百个。
我希望生成一个报告(最好使用 Azure Powershell 或 Azure CLI)以递归地提取分配给每个资源组中每个资源的所有标签的列表,用于 40-50 个订阅。
目前,我可以列出在资源组级别分配的所有标签,但我根本找不到一种方法来列出分配给每个资源组中各个资源的标签。我要提取此报告的订阅和资源组列表保存在一个 CSV 文件中,该文件包含两列,分别显示订阅名称和资源组。
任何关于如何实现上述目标的提示都将是非常棒的并且非常感谢。
没有详细的代码,但这里的想法。
1.You 应该写一个循环,在循环中,每次使用此 cmdlet 更改 subscription
:
Set-AzContext -Subscription $subscription_name
.
2.Then 使用此 cmdlet 获取指定 subscription
中的所有 resource group
:
$resource_groups = Get-AzResourceGroup
3.Then 编写一个嵌套循环(每个 resource group
循环),在这个嵌套循环中,使用此 cmdlet 获取 resource group
中的所有 azure resources
:
foreach($rg in $resource_groups){
$azure_resources = Get-AzResource -ResourceGroupName $rg.ResourceGroupName
}
4.Write 是步骤 3 中的另一个嵌套循环,此循环用于遍历指定 resource group
内的所有 azure resources
。然后使用下面的代码为 resource group
:
azure resource
获取标签
foreach($r in $azure_resources){
#the following code can get all the tags for one resource
$r.tags
}
基于伊万杨的逻辑。我已经构建了 PowerShell 脚本;
#------------声明变量-------------------------------- ---#
$bnsSubscription = Get-AzSubscription
$day = Get-Date -Format " ddMMMyyyy"
$tagPath = "C:\mytempfolder\"+"$day-Tag-Details.csv"
$tagFolderPath = "C:\mytempfolder\"
#------------声明变量-------------------------------- ---#
function Get-ResourceTag {
foreach ($subs in $bnsSubscription) {
Select-AzSubscription -SubscriptionName $subs.Name | Out-Null
Write-Host 'The selected Subscription is' ($subs).Name
New-Item -ItemType file -Path "$tagFolderPath$($subs.Name).csv" -Force
$resource_groups = Get-AzResourceGroup
$resource_groups_details = Get-AzResourceGroup | Sort-Location ResourceGroupName | Format-Table -GroupBy Location ResourceGroupName,ProvisioningState,Tags
Write-Host 'The selected Resource Group is' ($resource_groups).Name 'and the tag information as follows'
#$resource_groups_details
$resource_groups | Select-Object ResourceGroupName,Tags | Export-CSV -Path "$tagFolderPath$($subs.Name).csv" -Append
$OutputFile = @()
foreach($rg in $resource_groups){
$azure_resources = Get-AzResource -ResourceGroupName $rg.ResourceGroupName
$TestTags = $Resource.Tags.GetEnumerator()
foreach($r in $azure_resources){
Write-Host 'The selected resource is' ($r).Name 'and the information as follows'
$RGHT = New-Object "System.Collections.Generic.List[System.Object]"
$RGHT.Add("RGName",$r.ResourceGroupName)
$RGHT.Add("ResourceName",$r.name)
$RGHT.Add("Location",$r.Location)
$RGHT.Add("Id",$r.ResourceId)
$RGHT.Add("ResourceType",$r.ResourceType)
$RGHT.Add("ResourceTags",$r.Tags)
$OutputFile += New-Object psobject -Property $RGHT
$OutputFile | Export-Csv -Path "C:\mytempfolder\test22.csv" -append -NoClobber -NoTypeInformation -Encoding UTF8 -Force
}
}
}
}
#--------调用函数-------------------------------- ---#
Get-ResourceTag