BenAlabaster
BenAlabaster

Reputation: 39874

How do I retrieve the key value pairs from Get-AzureRmResourceGroup Tags

I have a resource group that is scheduled using a tag that has the key value pair: "IncludeInSchedule":"true"

When I do Get-AzureRmResourceGroup -Name MyResourceGroup I see:

ResourceGroupName : MyResourceGroup
Location          : northcentralus
ProvisioningState : Succeeded 
Tags              :
                    Name               Value
                    =================  ======
                    IncludeInSchedule  True
ResourceId        : /subscriptions/ea904806-082f-4ce5-9b66-288afd61f83e/resourceGroups/MyResourceGroup

When I attempt to read the value in the tag into a variable I'm coming unstuck. It looks like a hashtable, but Get-AzureRmResourceGroup -Name MyResourceGroup | Get-Member Tags suggests it's an Array of Hashtable, am I reading that right?

Name MemberType Definition                 
---- ---------- ----------                 
Tags Property   hashtable[] Tags {get;set;}

If I pipe the output from Get-AzureRmResourceGroup into Select-Object and expand the tags property I get:

Name  Value
===== =====
Value True      
Name  IncludeInSchedule

This is not what I expected to see, what I expected to see was:

IncludeInSchedule  True

Furthermore, when I attempt to assign the tags to a variable so I can extract the IncludeInSchedule value, I'm not seeing any values.

How do I extract the value from this?

Upvotes: 2

Views: 6075

Answers (2)

Till
Till

Reputation: 71

According to Microsoft's official documentation: Displaying Hash Tables

The following should work:

> (Get-AzureRmResourceGroup -Name MyResourceGroup).Tags.Value
True

You can get all tags' keys via:

> (Get-AzureRmResourceGroup -Name MyResourceGroup).Tags.Keys
Value
Name

For example, I am using this way to access uptimes for VMs like this:

> $vm = Get-AzureRmVM -ResourceGroupName $rgName -Name $vmName
> $vm.Tags.UptimeMonday
24h

In case you have any naming collisions with Microsoft's naming, they further state:

If the key name collides with one of the property names of the HashTable type, you can use PSBase to access those properties. For example, if the key name is keys and you want to return the collection of Keys, use this syntax

$hashtable.PSBase.Keys

Upvotes: 4

Frode F.
Frode F.

Reputation: 54981

Yes, Tags is an array of hashtables as defined by hashtable[] (notice square brackets).

Each object in the array is an hashtable like:

$t = @{ Name = "IncludeInSchedule"; Value = "True" }

$t

Name                           Value
----                           -----
Value                          True
Name                           IncludeInSchedule 

To access this from your objects, use:

$IncludeInSchedule = ((Get-AzureRmResourceGroup -Name MyResourceGroup).Tags | Where-Object { $_.Name -eq 'IncludeInSchedule'}).Value

#Or

$IncludeInSchedule = (Get-AzureRmResourceGroup -Name MyResourceGroup).Tags | Where-Object { $_.Name -eq 'IncludeInSchedule'} | ForEach-Object { $_.Value }

Output:

PS C:\Users\frode> $IncludeInSchedule
True

Upvotes: 0

Related Questions