Azure VM Tag automation

Cover

In my previous article we told about how to automate Azure VM stop/start on schedule using Resource Tags.

In this article we will talk about how to automate tag management. It is possible to manage tags via Microsoft Azure portal, but this is a long and ineffective way.

Tag automation functions

  • Introduce to you a pair of functions Get-AzVmTag/Add-AzVmTag from my Azure Automation Az-Module.
Import-Module Az-Module
Get-Command -Module Az-Module -Noun *tag
Get-Alias -Definition *-azvmtag
Get-Help Get-AzVmTag -Full
Get-Help Add-AzVmTag -Examples

Get-AzVmTag

  • The Get-AzVmTag function retrieves VM Tags. The -Tag parameter supports multiple tags.
Get-AzureRmVm -wa SilentlyContinue -ResourceGroupName (Select-AzResourceGroup) | Get-AzVmTag -Tag 'PowerOn', 'PowerOff'
Select-AzResourceGroup | Select-AzObject VM | Get-AzVmTag -Tags 'PowerOn', 'PowerOff'

Add-AzVmTag

The Add-AzVmTag function supports two Parameter Sets:

ParameterSet 1: Add a single tag

Parameter Description Default
-Tag Tag name No default
-Value Tag value No default
-Force Overwrite Tag if exists $false

Get-AzureRmVM -wa SilentlyContinue –ResourceGroupName (Select-AzResourceGroup) | Add-AzVmTag –Tag Notes –Value ‘Domain Controller’ –Force

Select-AzResourceGroup | Select-AzObject –ObjectType VM | Add-AzVmTag –Tag Project –Value ‘Project Name’

  • Tag names are case sensitive! This mean that you may accidentally create Tag named poweron even if you already have PowerOn Tag created. To avoid this kind mistakes the -Tag parameter restricted to set of values by [ValidateSet(IgnoreCase = $false)] attribute. You might want to edit this list to meet your needs.
[ValidateSet("PowerOn", "PowerOff", "Environment", "Project", "Notes", IgnoreCase = $false)]

01.Add-AzVmTag_Intellisense

  • If you will try to add already existing tag with no –Force parameter, you will get StatusCode TagExists.

02.Add-AzVmTag_Force

ParameterSet 2: Add a Power Tags pair

  • To add the PowerOn/PowerOff tags pair (the default Parameter set). If you omit any of the parameters, either -TagPowerOn or -TagPowerOff, the default value(s) will be set.
Parameter Description Default
-TagPowerOn PowerOn Tag value 11:11:11
-TagPowerOff PowerOff Tag value 22:22:22
  • Note that PowerOn/PowerOff tags always set in pair, even if you specify only one of them.
Get-AzureRmVM -wa SilentlyContinue –ResourceGroupName (Select-AzResourceGroup) | Add-AzVmTag –TagPowerOn ’06:00’ –TagPowerOff ’06:00 pm’
Select-AzResourceGroup | Select-AzObject –ObjectType VM | Add-AzVmTag 06:00 '06:00 pm'
sazrg | sazo VM | Add-AzVmTag 06:00 17:00
  • If you want to set only one Power-tag with no reset the second to the default value, use –Tag/-Value parameter set. But keep in mind, in this case the tag value will not be validated.

03.Add-AzVmTag_SinglePowerTag

Common parameters

Parameter Description Default
-VM VM(s), returned by Get-AzureRmVM No default
-Confirm Require confirm before Tag set $true
Get-AzureRmVM -wa SilentlyContinue | Add-AzVmTag –TagPowerOn ’06:00’ –TagPowerOff ’06:00 pm’ –Confirm:$false
Get-AzureRmVM -wa SilentlyContinue | Add-AzVmTag –Tag Environment –Value Production –Force –Confirm:$false

Helper functions

  • In all above examples I used two helper functions Select-AzResourceGroup and Select-AzObject from my Azure Automation Az-Module also. Both the functions based on the Write-Menu functionality.
Import-Module Az-Module
Get-Command -Module Az-Module –Verb select
Get-Alias -Definition select-az*
Get-Help Select-AzResourceGroup -Full
Get-Help Select-AzObject -Examples
Get-Help Select-AzObject -Parameter objecttype

Select-AzResourceGroup

  • Several hundred Microsoft AzureRm functions require or at least support -ResourceGroupName parameter. As a rule, these are most popular functions such Get-AzureRmStorageAccount, Get-AzureRmVm, Get-AzureRmVirtualNetwork, etc.
gcm -Module azurerm*
(Get-Command -ParameterName ResourceGroupName -Module azurerm* | measure -Line).Lines
Get-Command -ParameterName ResourceGroupName -Module azurerm* | select Name | sort Name | fw -Column 4
  • The Select-AzResourceGroup will assist you with these functions. You no longer need to memorize your ResourceGroup names.
Get-AzureRmVM -ResourceGroupName (Select-AzResourceGroup)
Get-AzureRmStorageAccount -ResourceGroupName (sazrg)

Select-AzObject

  • The Select-AzObject function allows you to interactively! (from menu) select a single object or its name (-NameOnly parameter) of particular object type (-ObjectType parameter).
Select-AzObject -ObjectType VNET-ResourceGroup (Select-AzResourceGroup) –NameOnly
Select-AzResourceGroup | Select-AzObject VM
  • The -ObjectType parameter supports Intellisense (Ctrl+Space or TAB completion).

  • The pipeline is supported as well.

Select-AzObject -ObjectType VM | Get-AzVmDisk
Select-AzResourceGroup | Select-AzObject VirtualNetwork | Get-AzSubnet
Select-AzResourceGroup | Select-AzObject VM | Get-AzVmPowerState
  • The functions with -Az suffix (Get-AzVmDisk, Get-AzSubnet, Get-AzVmPowerState), mentioned in the last example are from my Azure Automation Az-Module also and will be discussed in upcoming articles in the Az-Module Home category soon. Stay tuned.

You may also like:

Azure Automation – How to stop/start Azure VM on schedule
Azure Automation PowerShell Az-Module Home

4 thoughts on “Azure VM Tag automation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s