azure-powershell icon indicating copy to clipboard operation
azure-powershell copied to clipboard

Get-AzureRmResource returns wrong Type for Tags Property

Open ohwellwhatever opened this issue 8 years ago • 5 comments

Description

Get-AzureRmResource returns:

Tags Property System.Collections.Generic.IDictionary[string,string] Tags {get;set;}

Should be a hash table per https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-using-tags

Script/Steps for Reproduction


Module Version

ModuleType Version Name


Script 6.0.1 AzureRM
Script 5.6.0 AzureRM

Environment Data

Name Value


PSVersion 5.1.16299.431
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.16299.431
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

Debug Output


ohwellwhatever avatar May 10 '18 23:05 ohwellwhatever

@ohwellwhatever thanks for catching this -- looks like this is a result of a recent change we made to Get-AzureRmResource in AzureRM 6.0.1. We will update the corresponding docs as soon as we can to reflect these changes; have you ran into any other docs where you found this type of issue?

As a workaround in the meantime, you can run the following:

$r = Get-AzureRmResource -ResourceName {{yourResource}} -ResourceGroupName {{yourResourceGroup}}
$r.Tags.Add( {{key}} , {{value}} )
Set-AzureRmResource -ResourceId $r.ResourceId -Tag $r.Tags

cormacpayne avatar May 11 '18 00:05 cormacpayne

Get-AzureRmResourceGroup still returns the correct Type:

PS C:\source\AzurePosh> $rg = Get-AzureRmResourceGroup -ResourceGroupName TestRG PS C:\source\AzurePosh> $rg.Tags.GetType()

IsPublic IsSerial Name BaseType


True True Hashtable System.Object

We're applying RG tags to the Resources, so have to convert everything as such for a workaround:

[hashtable]$rTags = $r.Tags

Why the change? Seems to me it would be best to address it by reverting the change on Get-AzureRmResource.

Thanks

ohwellwhatever avatar May 11 '18 00:05 ohwellwhatever

@ohwellwhatever this change was made as a part of the breaking change release for AzureRM, which allowed us to update the API used for Get-AzureRmResource and Get-AzureRmResourceGroup. It looks like the Tags property that is returned from the server for both Resource and ResourceGroup is of type IDictionary<string, string>, but this is converted to a Hashtable for PSResourceGroup and remains a dictionary for PSResource. Let me see if this is able to be converted to a Hashtable as well and shipped with the next release.

cormacpayne avatar May 11 '18 00:05 cormacpayne

@ohwellwhatever update: our team discussed this issue and decided that we are going to keep the functionality as it is and look to see if we want to change it back to a Hashtable in the next breaking change release. Please feel free to contact me with any questions.

cormacpayne avatar May 18 '18 21:05 cormacpayne

@ohwellwhatever also, as an FYI, the document you linked to above has been updated to reflect the changes in the 6.0.0 release of AzureRM.Resources. Thanks for bringing this to our attention 😀

cormacpayne avatar May 18 '18 21:05 cormacpayne