Export All Azure Resources Tags

While playing the our customers Azure, I figured out that there is no way to identify the resource types apart from the naming convention. When you need generate a report to understand your resources, tags comes handy.

We always end up creating VMs in Azure using a script or through UI and forget to add Tags. Azure Tags are very helpful to define the resources. You apply tags to your Azure resources to logically organize them by categories. Each tag consists of a name and a value. For example, you can apply the name "Environment" and the value "Production" to all the resources in production. Without this tag, you might have difficulty identifying whether a resource is intended for development, test, or production. However, "Environment" and "Production" are just examples. You define the names and values that make the most sense for organizing your subscription.

After you apply tags, you can retrieve all the resources in your subscription with that tag name and value. Tags enable you to retrieve related resources that reside in different resource groups. This approach is helpful when you need to organize resources for billing or management.

ref: https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-using-tags

Now, here is a PowerShell script I developed to dump all the resounces with their tags and value into an excel. I was able to populate this data in PowerBI to create a report which I will explain in the next article.

# This script loops through all azure subscriptions, resource groups and resources to print tags in an excel.

# Please create an empty excel file at C:\temp\tags.xlsx

Set-ExecutionPolicy -ExecutionPolicy unrestricted -Force

$excel = New-Object -ComObject excel.application

$excel.visible = $True

$workbook = $excel.Workbooks.Add()

$workbook.Worksheets.Add()

$Data= $workbook.Worksheets.Item(1)

$Data.Name = 'MySpreadsheet'

#$file="C:\temp\Tags.csv"

$data.Cells.Item(1,1) = "Name"

$data.Cells.Item(1,2) = "ResourceId"

$data.Cells.Item(1,3) = "ResourceName"

$data.Cells.Item(1,4) = "ResourceType"

$data.Cells.Item(1,5) = "kind"

$data.Cells.Item(1,6) = "ResourceGroupName"

$data.Cells.Item(1,7) = "Location"

$data.Cells.Item(1,8) = "SubscriptionId"

$data.Cells.Item(1,9) = "Tags"

$data.Cells.Item(1,10) = "SKUName"

$data.Cells.Item(1,11) = "SKUTier"

#$d = $data.UsedRange

#$d.Interior.ColorIndex = 19

#$d.Font.ColorIndex = 11

#$d.Font.Bold = $True

$intRow = 2

Login-AzureRmAccount

foreach($sub in Get-AzureRmSubscription)

{

Set-AzureRmContext -Subscription $sub.Id

#$sub.Name

foreach ($res in Get-AzureRmResource)

{

#$t=(Find-AzureRmResource -ResourceGroupNameEquals $res.ResourceGroupName -ResourceNameEquals $res.ResourceName).Tags

#$t.Keys

#$t.Values

$data.Cells.Item($intRow, 1) = $res.Name

$data.Cells.Item($intRow, 2) = $res.ResourceId

$data.Cells.Item($intRow, 3) = $res.ResourceName

$data.Cells.Item($intRow, 4) = $res.ResourceType

$data.Cells.Item($intRow, 5) = $res.kind

$data.Cells.Item($intRow, 6) = $res.ResourceGroupName

$data.Cells.Item($intRow, 7) = $res.Location

$data.Cells.Item($intRow, 8) = $res.SubscriptionId

$data.Cells.Item($intRow, 11) = $res.sku.name

$data.Cells.Item($intRow, 12) = $res.sku.tier

if($res.Tags -ne $null)

{

$tag=@()

$res.Tags.GetEnumerator() | ForEach-Object {

#[string]$key += $_.key+";"

#[string]$value += $_.value+";"

[string]$tag +=$_.key+"="+$_.value+";"

$data.Cells.Item($intRow, 9)=$tag

}

$intRow = $intRow + 1

}

}

}

$usedRange = $Data.UsedRange

$usedRange.EntireColumn.AutoFit() | Out-Null

$workbook.SaveAs("C:\temp\tags.xlsx")

$excel.Quit()

$a.Save()

$a.Close()

$Excel.Quit()

[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel)

Stop-Process -Name EXCEL -Force

Please comment if you have any questions.

Comments

  • Anonymous
    October 29, 2018
    Hi Rajeev, Thank you for sharing very useful script indeed. when is the part 2 of this article coming ?