7

In my Azure dev/test lab (DTL), there are many resources which were not tagged. How can I get a list of all untagged resources under DTL/resource group?

DevX
  • 655
  • 1
  • 11
  • 25

5 Answers5

6

Here's a simple PowerShell loop to get untagged resources.

$resources = Get-AzureRmResource
foreach($resource in $resources)
{
    if ($resource.Tags -eq $null)
    {
        echo $resource.Name, $resource.ResourceType
    }
}

Other ways to query this information and also set tags programmatically or as part of resource deployments are described here.

If you want to avoid the situation of ending up with untagged resources, you could enforce a customized policy that all resources should have a value for a particular tag.

huysmania
  • 1,030
  • 3
  • 11
3

Here is the idiomatic PowerShell to supplement @huysmania's answer which is expressed in procedural language mindset (and updated for the new PowerShell Az cmdlets):

Get-AzResource | Where-Object Tags -eq $null | Select-Object -Property Name, ResourceType

and the terse (alias) form:

Get-AzResource | ? Tags -eq $null | select Name, ResourceType
JohnC
  • 1,449
  • 16
  • 25
0

I usually just run this command to output a table of untagged resources using Get-AzResource. It filters Azure resources with tags that are $null or empty using Where-Object.

Get-AzResource `
    | Where-Object {$null -eq $_.Tags -or $_.Tags.Count -eq 0} `
    | Format-Table -AutoSize

If you want to list untagged resources for a specific resource group, you can just add the -ResourceGroupName switch to Get-AzResource.

$resourceGroupName = "My Resource Group"

Get-AzResource -ResourceGroupName $resourceGroupName `
    | Where-Object {$null -eq $_.Tags -or $_.Tags.Count -eq 0} `
    | Format-Table -AutoSize

Note: The above uses the newer Azure PowerShell Az module, which is replacement for AzureRM.

RoadRunner
  • 23,173
  • 5
  • 28
  • 59
-1

This link has the solution for this question. It beautifully explains assigning and querying tags using powershell.

$resourceGroupName = 'InternalReportingRGDev'

$azureRGInfo = Get-AzureRmResourceGroup -Name $resourceGroupName foreach ($item in $azureRGInfo)

{

Find-AzureRmResource -ResourceGroupNameEquals $item.ResourceGroupName | ForEach-Object {Set-AzureRmResource -ResourceId $PSItem.ResourceId -Tag $item.Tags -Force }

}

DevX
  • 655
  • 1
  • 11
  • 25
-1

<#Bellow is PowerShell script to locate untagged resources - you may change the script out put as per your requirement. Hope must be helpful. Thanks!#>

Write-Host "List all resource where Tag value is not Set"
Write-Host "********************************************"
#Fetch all resource details
$resources=get-AzureRmResource
foreach ($resource in $resources) {
    $tagcount=(get-AzureRmResource | where-object {$_.Name -match $resource.Name}).Tags.count
    if($tagcount -eq 0) {
        Write-Host "Resource Name - "$resource.Name
        Write-Host "Resource Type and RG Name : " $resource.resourcetype " & " $resource.resourcegroupname "`n"
    }
 }
  • `$resource` already has the tag member. No need for `$tagcount=(get-AzureRmResource | where-object {$_.Name -match $resource.Name}).Tags.count`. This is innefficient and slow because it has to search every resource to find the tag for the current resource. You can get the count simply with `$tagcount = $resource.Tags.Count`. – RoadRunner Apr 14 '20 at 17:42