Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 683
Troy Hunt Avatar asked Nov 15 '25 22:11

Troy Hunt


2 Answers

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

like image 98
Simon W Avatar answered Nov 18 '25 13:11

Simon W


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.

like image 29
Loul G. Avatar answered Nov 18 '25 13:11

Loul G.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!