Azure Resource Manager Tags – Extending tag functionality with JSON string


Hello Everyone, this is Paulo.

Introduced with Azure Resource Manager, resource tags helps us organizing resources, it is a well known resource also for building charge back process, where shared resources that resides in different resource groups can receive the same tag. For more information about Azure Resource Tags, please refer to this article.

Tags are simply a key and value pair that you can assign to resources, e.g. Key => dept, Value => Information Technology. E.g.

Tags00

But what if we want to make these tags a little bit more smarter and consume them with Powershell as Powershell Object exposing more properties?

The solution for that is creating a tag with a JSON string, then later we can work with Powershell and read it as Powershell object.

In order to test it, let’s first define our JSON structure like this:

{

"Name":"Paulo"
"LastName":"Marques"

}

Lets make it a single line like this: {“Name”:”Paulo”,”LastName”:”Marques”}

In your subscription, through the new portal (https://portal.azure.com), browse to any resource. In this example I’m selecting an Azure DNS Zone resource, follow these steps to add your structured Tag:

Tags03

  1. Click on Tag icon
  2. Enter the key name on Key field, myJsonTag in this example
  3. Paste the JSON string on Value field
  4. Save it.

 

Note:

This blog post assumes you already have Azure Powershell 1.0 or greater module installed in your computer, if not, install it from https://aka.ms/webpi-azps.

 

Now that we tagged our resource, lets see what we can do on Powershell with that tag that is a JSON structure, please open a Powershell command prompt and follow these steps:

  1. Type Add-AzureRMAccount to authenticate to your Azure subscription.
  2. Type Select-AzureRmSubscription -SubscriptionName <your subscription name> to select current subscription to work with.
  3. Type $resource = Get-AzureRmResource -ResourceName “<your resource name>” -ResourceGroupName “<your resource group name>” and enter to get your resource as an object.

    Tags02

  4. Note the Tags property of my $resource object, it is an array of hash tables. In order to get the list of tags associated with that object, type $resource.Tags

    Tags04

  5. Tags on resources are returned as an array of has tables, so in order to access the tag we want directly, we need to apply a filter, type $tag = $resource.Tags | ? { $_.Values -eq “myjsontag” } and press Enter. Type $tag to see the contents of your tag.

    Tags05

  6. If you are tagging a resource group, the tags are shown differently as in the following example (this is just to illustrate how we access the same type of tag from a resource group, you don’t need to follow this step) but they are filtered in the same way.

    Tags06

  7. To get the JSON defined structure, type $tag.Value

    Tags07

  8. This is not yet a Powershell object, to get an actual Powershell object, type $myTagObj = $tag.Value | ConvertFrom-Json

    Tags08

  9. And this becomes your Powershell object, to get the LastName, type $myTagObj.LastName

    Tags09

That’s it for this article, thanks.

Paulo

Comments (4)

  1. Jason Ghazai says:

    It would be nice for articles like this to suggest why this might be a better way to use tagging. Can you provide some details around how this method will improve manageability of tagging vs. the standard way of tagging?

  2. Hi Jason,

    I have a real example for that, in the next weeks I will blog in more detail about it. For now, think about this scenario, a few weeks ago I had a request from a customer asking me to help them implementing an Azure Automation runbook to enable a weekly scheduled
    startup and shutdown of some VMs, so one of the solutions was to tag the VM with a JSON based string that structures the days of weeks, startup time and shutdown time plus which time zone that VM belongs, in that way the runbook can read the JSON structure
    and decide what to do with that particular VM.

    Hope that helps stay tuned for that article.

  3. Raj says:

    Paulo, Iam exactly looking for similar scenario and I will wait for your article soon

Skip to main content