Troy Hunt
Troy Hunt

Reputation: 20387

Can't set a tag on an Azure SQL Database using PowerShell

You can set a tag on an Azure resource such as a website by using Set-AzureResource:

Set-AzureResource -Name [site name] -ResourceGroupName [resource group name] -ResourceType Microsoft.Web/sites -ApiVersion 2014-04-01 -Tags @{ Name = [my tag name]; Value = [my tag value] }

But no matter what I try, I can't get this working on an Azure SQL DB. In fact I can't even select the resource - here's what I'm trying:

Get-AzureResource -Name TroyPSTestDB -ResourceGroupName Default-SQL-WestUS -ResourceType Microsoft.Sql/servers -ApiVersion 2014-04-01

I always get "Provided resource does not exist". However, I can see the resource when I select all databases:

Get-AzureResource -ResourceGroupName Default-SQL-WestUS -ResourceType Microsoft.Sql/servers/databases

Which gives me:

Name              : TroyPSTestDB
ResourceGroupName : Default-SQL-WestUS
ResourceType      : Microsoft.Sql/servers/databases
ParentResource    : servers/snyb5o1pxk
Location          : westus
Permissions       :
                    Actions  NotActions
                    =======  ==========
                    *

ResourceId        : /subscriptions/62e2a1e5-4eda-4c1e-805e-44a6c8f8afbd/resourceGroups/Default-SQL-WestUS/providers/Microsoft.Sql/servers/snyb5o1pxk/databases/TroyPSTestDB

So what gives? Have I got something wrong with the PS command? Or can you simply not select a single DB this way? Any other way to get the tag on it? Thanks!

Upvotes: 4

Views: 1869

Answers (2)

Loul G.
Loul G.

Reputation: 1095

With AzureRM.Resources 3.0.1, you can set the tags on a Sql Database using the following syntax:

Set-AzureRmResource -ResourceType 'Microsoft.Sql/servers/databases' -ResourceName "$sqlServerName/$sqlDatabaseName" -ResourceGroupName $ressourceGroupName -Tag @{ env = "dev" }

Actually, the name of the Sql database resource returned by Get-AzureRmResource already has the format server/database.

Upvotes: 1

Simon W
Simon W

Reputation: 5496

UPDATE: Not so much a bug as gotcha with the arguments supplied:

Get-AzureResource -Name {dbname} -ParentResource servers/{servername} -ResourceType Microsoft.Sql/servers/databases -ResourceGroupName {resource-group-name} -ApiVersion 2014-04-01

The key is to include 'servers/' in the -ParentResource argument.

So, to set Tags you need to make sure that you set -ParentResource correctly - the remainder of your arguments are right.

===

You can add tags via the new Portal. The Powershell stuff for managing this looks to have a bug.

In portal: /subscriptions/GUID/resourceGroups/Default-SQL-WestUS/providers/Microsoft.Sql/servers/{servername}/databases/{database}?api-version=2014-04-01

In PS: /subscriptions/GUID/resourcegroups/Default-SQL-WestUS/providers/Microsoft.Sql/{servername}/databases/{database}?api-version=2014-04-01

(note missing servers node in PS command).

Issue raised: https://github.com/Azure/azure-powershell/issues/73

Upvotes: 3

Related Questions