Skip to content

Instantly share code, notes, and snippets.

@srpomeroy
Created November 26, 2018 21:33
Show Gist options
  • Save srpomeroy/5cf1390251319fc9c4c6b4f91be47820 to your computer and use it in GitHub Desktop.
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.
$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