Wednesday, November 2, 2016

Azure ARM Tagging a Resource Group with PowerShell Commands



 

<#

Here is your PowerShell command for tagging an existing Resource group
 

1. Provide your resource group name in “YourResourceGroupName”

2. Define the tags you need examples: Region, App Name, Department, Environment, etc.

3. Resource Types – you need to add the resource type for which you need to tag into the Where section ex: “Microsoft.Compute/availability Sets will only tag Availability Sets with a Resource group.

4. To make your life easier I have listed few common Resource Types which you can add into the script

#>

 

"Microsoft.Network/connections"

"Microsoft.Network/networkSecurityGroups"

"Microsoft.Network/publicIPAddresses"

"Microsoft.Network/routeTables"

"Microsoft.Network/virtualNetworkGateways"

"Microsoft.Network/networkInterfaces"

"Microsoft.Network/publicIPAddresses"

"Microsoft.Compute/availabilitySets"

"Microsoft.Network/loadBalancers"

"Microsoft.Compute/virtualMachines"

 

 

# Here is the simple Script!

 

$RGName = "YourResourceGroupName"

Set-AzureRmResourceGroup -Name $RGName -Tag @{Region="AMERICAS"; Division="IT"; Environment ="PROD"; Group= "IT-DEPT"; ServiceGroup= "IT-NETWORK" }

 

$Tag = (Get-AzureRmResourceGroup -Name $RGName).Tags

Get-AzureRmResource |

 

Where {$_.ResourceType -eq "Microsoft.Compute/availabilitySets" -or "Microsoft.Compute/virtualMachines"-or "Microsoft.Network/networkInterfaces" -and $_.ResourceGRoupName -eq $RGName} |

ForEach-Object {Set-AzureRmResource -Tag $Tag -ResourceId $_.ResourceId -force}