Created
November 26, 2018 21:33
-
-
Save srpomeroy/5cf1390251319fc9c4c6b4f91be47820 to your computer and use it in GitHub Desktop.
Example snippet that tags all AzureRM resources with the name of the resource group they are in.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$tagKey = "resourcegroup" | |
$resources = Get-AzureRmResource | |
foreach ($r in $resources) | |
{ | |
$resourceGroupName = $r.ResourceGroupName | |
$resourceTags = $r.tags | |
if ($resourceTags) { | |
if($resourceTags.Keys -contains $tagKey) { | |
if($resourceTags[$tagKey] -ne $resourceGroupName) { | |
$resourceTags.Remove($tagKey) | |
$resourcetags.Add($tagKey, $resourceGroupName) | |
Set-AzureRmResource -Tag $resourcetags -ResourceId $r.ResourceId -Force | |
} | |
} | |
else { | |
$resourcetags.Add($tagKey, $resourceGroupName) | |
Set-AzureRmResource -Tag $resourcetags -ResourceId $r.ResourceId -Force | |
} | |
} | |
else { | |
Set-AzureRmResource -Tag @{$tagKey=$resourceGroupName} -ResourceId $r.ResourceId -Force | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment