1

I am trying to programmatically add tags to resources in Azure Government. When I try to set the tags on a resource that has no tags I am using the Set-AzureRmResource command. I have tried both setting the ApiVersion and without (without is supposed to use the latest) When I use the Debug flag it shows the version being set but I still get an error below.

Set-AzureRmResource : Cannot validate argument on parameter 'ResourceId'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
At line:1 char:108
+ ... ONMENT=""; ORGANIZATION="" } -ResourceId $resource.ResourceId  -Force ...
+                                              ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-AzureRmResource], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implem
   entation.SetAzureResourceCmdlet

The snippet I am trying to run is below.

Set-AzureRmResource -Tag @{ ENVIRONMENT=""; ORGANIZATION="" } -ResourceId $resource.ResourceId  -Force

Edit: Specify Azure Government Edit 2: Removed explicit version setting from code

Darroll
  • 438
  • 4
  • 10
  • I have. It does contain what I think it should. I'm calling it in a foreach loop. I even tried the cross-platform Az module as well – Darroll Jan 29 '19 at 14:24
  • I output the variable as part of debugging and it is not empty. I'm trying other options for setting the tag now. Will let you know. – Darroll Jan 29 '19 at 14:36
  • I also want to add this same error happens without the loop. It isn't the loop, and the value of the ResourceId is not null. – Darroll Jan 29 '19 at 14:42
  • FYI, this works in Commercial Azure, Only failing in Azure Government – Darroll Jan 29 '19 at 14:53
  • never worked with that, I'm in another part of the world, but it might be something fishy going on there with the apis. it would've probably helped if you mentioned this in the question. probably worth raising an issue on GH – 4c74356b41 Jan 29 '19 at 14:58

3 Answers3

1

I tried using the new Az commands in Azure Government:

Set-AzResource -Tag @{ ENVIRONMENT=""; ORGANIZATION="" } -ResourceId $resource.ResourceId  -Force

..and it worked fine for me. (I got a serialization error using the older AzureRm commands). FYI... not sure when you tried it yesterday, but there was a DNS issue that caused some service outages, which may have resulted in erroneous errors.

Mike
  • 346
  • 1
  • 6
1

I tested your snippet above against an Azure Government resource and its working correctly using AzureRM module version 6.13.1.

You can your version by running this snippet

Get-Module AzureRM -List
Kyle Deeds
  • 11
  • 2
0

For anyone who sees this in the future. The problem is a known bug in the way Azure encodes the '#' symbol in a ResourceId. So do not use '#' in a resource string.

The code change we are making is to use the following:

 $resource.ResourceId = $resource.ResourceId.Replace("#", "%23")

Simple fix if you have the # symbol, or change your governance to not name things with '#'.

Thanks for all the other replies. We had to open a ticket to get this information.

Darroll
  • 438
  • 4
  • 10