As as system admin/DevOps guys, usually during audit time we need to complete the finding (by make the grouping to filter) in faster manner instead of doing as manual work, like similar we got the review command that all the resources need to be have the tags. But completing this task will have huge manual effort as we maintaining lot of resources so thought to make automation to complete this task. Here we going to discuss bout how to Add Tags On An AZURE SQL DATABASE Using PowerShell
What Is A Tag In Azure?
Its Creates a predefined Azure tag or adds values to an existing tag | Creates or updates the entire set of tags on a resource or subscription.
Azure tagging is an excellent feature from Microsoft that was help you to logically group your Azure resources and help you to track your resources. It also helps to automate the deployments of the resources and another important feature is this feature helps to provide the visibility of the resource costs that they are liable for.
Syntax: New-AzTag [-ResourceId] <String> [-Tag] <Hashtable> [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
What are the ways to create a Tags in Azure?
The Azure Tag enables the key-value pairs to be created and assigned to the resources in Azure using Azure Portal, PowerShell. Azure CLI, etc.
Note: Tag names and Tag Values are case-sensitive in nature.
Why Use The Azure tag ?
The main intention to use the Azure tags is to organize the resources in Azure Portal. When you are organizing the resources properly helps you to identify the category that the resources belong to. So basically, Azure tags are the name-value pairs that help to organize the Azure resources in the Azure Portal
For example (consider you having the tag), When you have more resources in your Azure Portal, that time it really helps you to categorize your Azure Resources. Suppose you have 6 Virtual machines (VM) in your Azure Subscription, among them 2 are under our development environment and 2 are for the QA environment and remain 2 belong to our Production environment. So we can tag them as Environment = Development, Environment = QA, or Environment = Production. so now we can easily get the view of each resource coming under the specific environment.
How To Create Azure Tags Using powershell
Step 1: Connect to your Azure account using Connect-AzAccount
Before starting please gather secret for service principal , AppId , tenantId to establish the connect to the azure portal to perform the operation against the Azure services.
1 2 3 4 5 6 7 8 9 10 11 12 |
#Converts plain text or encrypted strings to secure strings. $SecureServicePrinciple = ConvertTo-SecureString -String "rfthr~SSDCDFSDFE53Lr3Daz95WF343jXBAtXADSsdfEED" -AsPlainText -Force #Assigning the App ID $AppId = "0ee7e633-0c49-408e-b956-36d62264f644" #Assigning the Tenant ID $tenantId= "32cf8ba2-403a-234b-a3b9-63c2f8311778" #storing a username and password combination securely. $pscredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $AppId, $SecureServicePrinciple #Connect to Azure with an authenticated account for use with cmdlets from the Az PowerShell modules. Connect-AzAccount -ServicePrincipal -Credential $pscredential -Tenant $tenantId |
Step 2: Define the tags and assign as a array list
Based on my requirement i had added the below tags, you can create N number of tags based on your segregation of the resources.
1 2 3 |
$tags = @{"Business Unit"="WX_Digital"; "Environment"="PROD"; "Owner"="dotnet-helpers" ; "Project"="webapp"} |
Step 3: Getting all the SQL database in the specific resources group
Example 1: Get single SQL database to update
- Get-AzResource cmdlet will Gets All the resources from the specific subscription and filtered with Resource Type SQL.
- After executing below script the RESOURCE.id variable will have all the databases names inside the specific resource type as shown in the below snap shot.
- -ResourceType : The resource type of the resource(s) to be retrieved. For example, Microsoft.Compute/virtualMachines
1 2 3 4 5 6 |
#GET the single database by where condition $RESOURCE = Get-AzResource -ResourceGroupName "rg-dgtl-pprd" -ResourceType "Microsoft.Sql/servers/databases" | Where-Object name -Like 'sqlsrvr-dgtl-pprd/sitecore_master' $resourceIds = $RESOURCE.Id |
Example 2: Get all the database to update
1 2 3 4 5 6 |
#Gets All the database $RESOURCE = Get-AzResource -ResourceGroupName "rg-dgtl-pprd" -ResourceType "Microsoft.Sql/servers/databases" $resourceIds = $RESOURCE.Id |
Step 4: Update new tags using new-AzTag command
We can Creates a predefined Azure tag or adds values to an existing tag using new-AzTag cmdlet | Creates or updates the entire set of tags on a resource or subscription.
-ResourceId : The resource identifier for the entity being tagged. A resource, a resource group or a subscription may be tagged.
Example: 1 Add tags to single database
1 2 3 4 5 |
#Creates or updates the entire set of tags on a resource or subscription. #The resource identifier for the entity being tagged. A resource, a resource group or a subscription may be tagged. new-AzTag -ResourceId $resourceId -Tag $tags |
Example 2: Add tags to all database under ResourceGroup
1 2 3 4 5 6 7 8 9 |
foreach($resourceId in $resourceIds){ Write-Output $resourceId #Creates or updates the entire set of tags on a resource or subscription. new-AzTag -ResourceId $resourceId -Tag $tags } |
Output:
Note:
- Get-AzResource cmdlet will Gets All the resources from the specific subscription and filtered with Resource Type SQL.
- -ResourceType : The resource type of the resource(s) to be retrieved. For example, Microsoft.Compute/virtualMachines
Leave A Comment