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?

4

5 回答 5

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.

于 2017-09-15T07:36:52.113 回答
4

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
于 2019-12-19T19:48:07.420 回答
0

<#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"
    }
 }
于 2018-05-23T06:00:25.217 回答
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.

于 2020-04-14T17:29:02.873 回答
-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 }

}

于 2017-09-24T19:40:38.607 回答