Tags

PowerShell: 101-vm-tags

This is a conversion of ARM template 101-vm-tags from the GitHub repository azure\azure-quickstart-templates to PowerShell Script, and this script will deploy following the resources...


Terraform: 101-vm-tags

This is a conversion of ARM template 101-vm-tags from the GitHub repository azure\azure-quickstart-templates to Terraform configuration, and this configuration will deploy following the resources...


Find Azure Resources By Tags Using PowerShell

Recently, I have been requested to write a small PowerShell script to fetch all the Azure resources with no tags at all, but I thought of writing a comprehensive script that is not only to find all the resources with no tags but also to find resources with specific tag name(s), tag value(s), tag(s), or with all tags. Tagging in Azure I have already covered Tagging Microsoft Azure Resources Using Powershell (Az)  in my previous post, but just to brief… Tags in Azure play pivotal role in managing the resources, predominantly in the cost governance strategies and much useful for automation and maintain environment hygiene. More than a resource name, tagging is very crucial and it must be consistent and appropriate across the resources in all the resource groups and subscriptions. Many organizations leverage the tagging effectively and consistently using the Azure policies or some automation techniques. Find-AzResource However, finding the resources in Azure is also crucial, and especially finding all the resources of all types from multiple subscriptions or resource groups. So I have come up with a PowerShell script to find all the Azure tagged/not tagged resources, and you can find the script in my GitHub repo…  View the code in GitHub   The script comes with an in-build help, and if you run the script without any parameters it will display the help as below… C:\Users\kiran\PSScripts> . .\Find-AzResource.ps1 C:\Users\kiran\PSScripts> Find-AzResource Output: NAME Find-AzResource SYNOPSIS Find-AzResource gets all the Azure tagged/not tagged resources, SYNTAX Find-AzResource [-ResourceName <String[]>] [-Location <String[]>] [-ResourceType <String[]>] [<CommonParameters>] Find-AzResource -ResourceGroupName <String[]> [-ResourceName <String[]>] [-Location <String[]>] [-ResourceType <String[]>] -Tag <Hashtable> [<CommonParameters>] Find-AzResource -ResourceGroupName <String[]> [-ResourceName <String[]>] [-Location <String[]>] [-ResourceType <String[]>] -TagValue <String[]> [<CommonParameters>] Find-AzResource -ResourceGroupName <String[]> [-ResourceName <String[]>] [-Location <String[]>] [-ResourceType <String[]>] -TagName <String[]> [<CommonParameters>] Find-AzResource -ResourceGroupName <String[]> [-ResourceName <String[]>] [-Location <String[]>] [-ResourceType <String[]>] -WithNoTag [<CommonParameters>] Find-AzResource -ResourceGroupName <String[]> [-ResourceName <String[]>] [-Location <String[]>] [-ResourceType <String[]>] -AllTagged [<CommonParameters>] Find-AzResource -SubscriptionName <String[]> [-ResourceName <String[]>] [-Location <String[]>] [-ResourceType <String[]>] -Tag <Hashtable> [<CommonParameters>] Find-AzResource -SubscriptionName <String[]> [-ResourceName <String[]>] [-Location <String[]>] [-ResourceType <String[]>] -TagValue <String[]> [<CommonParameters>] Find-AzResource -SubscriptionName <String[]> [-ResourceName <String[]>] [-Location <String[]>] [-ResourceType <String[]>] -TagName <String[]> [<CommonParameters>] Find-AzResource -SubscriptionName <String[]> [-ResourceName <String[]>] [-Location <String[]>] [-ResourceType <String[]>] -WithNoTag [<CommonParameters>] Find-AzResource -SubscriptionName <String[]> [-ResourceName <String[]>] [-Location <String[]>] [-ResourceType <String[]>] -AllTagged [<CommonParameters>] DESCRIPTION Find-AzResource gets all the Azure resources with... > All tags > No tags > Specific tag name(s) > Specific tag value(s) > Specific tag(s) ... from one or more resourcegroup(s) or subscripttion(s) and optionally filter the resources by location, name and type as well. ... output truncated ... -------------------------- EXAMPLE 1 -------------------------- PS > Find-AzResource Displays full help -------------------------- EXAMPLE 2 -------------------------- PS > Find-AzResource -SubscriptionName Sub1, Sub2 -AllTagged Finds all the resources with tags in the given Subscriptions. it even works with ResourceGroupName as well. Optionally, you can even filter the resources by Name, Location and Type. -------------------------- EXAMPLE 3 -------------------------- PS > Find-AzResource -SubscriptionName Sub1, Sub2 -WithNoTag Finds all the resources with no tags in the given Subscriptions. It even works with ResourceGroupName as well. Optionally, you can even filter the resources by Name, Location and Type. -------------------------- EXAMPLE 4 -------------------------- PS > Find-AzResource -ResourceGroupName RG1, RG2 -TagName Status Finds all the resources with given tag name in the given resource groups. It even works with the subscriptions as well. Optionally, you can even filter the resources by Name, Location and Type. -------------------------- EXAMPLE 5 -------------------------- PS > Find-AzResource -ResourceGroupName RG1, RG2 -TagValue HR, Finance Finds all the resources with given tag values in the given resource groups. It even works with the subscriptions as well. Optionally, you can even filter the resources by Name, Location and Type. -------------------------- EXAMPLE 6 -------------------------- PS > Find-AzResource -ResourceGroupName RG1, RG2 -Tag @{Dept='IT'; Status="Expired"} Finds all the resources with given tags in the given resource groups. It even works with the subscriptions as well. Optionally, you can even filter the resources by Name, Location and Type.  View the code in GitHub  


Tagging Microsoft Azure Resources Using Powershell (Az)

In Azure Cloud, Tags play a major role to manage resources in an easy way, in an other words Tags are an additional meta data associated with the Azure resources. We can assign the tags to the individual resources like VM, Storage Account, VNet and etc., and we can also assign the tags to the Resource Groups as well. Resource groups allow us to organize the related resources together and facilitate the management, but tags are used to group the resources beyond the resource groups including the resource groups, and at the same time resources inside the resource group do not inherit the tags associated with the resource group. Tags are Key and Value combination that can be assigned to the resources in the Azure cloud, for example… Tag Key Tag Value ResourceType VM Project MyProject Department Marketing Environment Production CostCenterCode 123456 Do bear in mind that each individual resource can have up to 15 tags max (Microsoft keeps updating the numbers time to time, so please refer the Microsoft Docs for the exact number), and ensure the tags are unique and consistent naming convention among Azure resources. Tags are used to organize the deployed resources in the Azure cloud, we could search the resources by tag key/value, for example search the resources with the tags associated {Key:Value} Type:VM and Environment:Production, then the search results all the production VMs across the resource groups within a subscription. Tags are also used to view the related resources, like all the resources tagged to a specific project or a specific cost center and to facilitate the billing and cost management. Tags can be created at the time of creating resources or at the later time by using the Azure portal or any command line tools like PowerShell or Azure CLI. Let’s see how we can create and manage the tags using PowerShell… #requires -Module Az # Connect-AzAccount ### Add new tags to an existing resource # Get the resource $Resource = Get-AzResource -Name testvm -ResourceGroupName test-rg # Resource tags [hashtable] $Tags = $Resource.Tags # Ensure not to overwrite the tags if ($null -eq $Tags) { [hashtable] $Tags = @{Type="VM"; Environment="Test"} } else { $Tags += @{Type="VM"; Environment="Test"} } # Add new tags to the resource (-Force to override the confirmation if there are any existing tags) Set-AzResource -ResourceId $Resource.Id -Tag $Tags -Force ### Remove an existing tag / remove all tags from a resource # Get the resource $Resource = Get-AzResource -Name testvm -ResourceGroupName test-rg # Resource tags [hashtable] $Tags = $Resource.Tags # Remove the specific tag $Tags.Remove("Type") # Overwrite the remaining tags to the resource (-Force to override the confirmation if there are any existing tags) Set-AzResource -ResourceId $Resource.Id -Tag $Tags -Force ## Remove all tags Set-AzResource -ResourceId $Resource.Id -Tag @{} -Force ### List all the resources with a specific tag key Get-AzResource -TagName "Environment" ### List all the resources with a specific tag value Get-AzResource -TagValue "Test" ### List all the resources with a specific tag key and value Get-AzResource -Tag @{Environment="Test"} ### List all the tags and number of resources associated in a subscription. Get-AzTag