Automation | VM Tags Creation

Overview

Tagcreation.ps1 is a PowerShell script to create tags for all VMs provided in the input csv, Azure tag is an important feature that helps you to identify the deviation and make them standard. Also you can make use of tags in the Azure automation runbook to customise many automation task and schedule them.

Note: Make sure you provide all the tags and tag values even if you want to modify just one value. Azure didn’t make this user friendly.

The following guide assumes that the user has basic knowledge of using Windows and PowerShell.

Although some familiarity with the Azure Portal would be beneficial, it is absolutely not necessary, as a step by step approach will be provided.

Script workflow:

  • Collects all the input from csv
  • Select subscription
  • Validate VM name exist in the subscription if does exists then skips that particular VM.
  • Apply the tags to validated VMs
  1. Pre-requisites

The following prerequisites must be met, before performing the Core capacity check steps.

  1. Internet connection
  2. Valid Azure Subscription
  3. The person who will perform the below steps must have one of the following roles in the Azure subscription
    • Owner
    • Contributor
  4. Some familiarity with the Azure Portal would be beneficial, although not necessary
  5. Latest version of Microsoft .NET Framework (download link)
  6. Latest version of Microsoft Windows Management Framework (download link)
  7. Latest version of Microsoft PowerShell (download link including detailed installation guide)
  8. Latest version of Azure cmdlets (installation guide), although steps are provided below
  9. Some familiarity with PowerShell ISE, although not necessary (documentation)
  10. Some familiarity with the Azure Portal would be beneficial, although not necessary
  11. Some familiarity with PowerShell scripting would be beneficial, although not necessary
  12. Tag Creation Script

Windows PowerShell scripts can also be used for creating the tags for VM’s deployed in azure portal. It can be used to create multiple tags for multiple VM’s in different subscriptions. Windows Azure PowerShell module should be installed to use azure based commend lets (as mentioned in pre requisites).

Follow the following link to download and configure the module – https://azure.microsoft.com/en-in/documentation/articles/powershell-install-configure

  • Script content can be found at the end of this blog. For using this script,
    1. Open PowerShell ISE and type below command

# connecting your azure account to powershell

Add-AzureRMaccount

It gives the following popup

  1. Enter your credentials and click sign in. Once your account is added, just open the CSV file attached with script you downloaded.
  2. Add the VM details as required and save the file before closing it
  3. Now go back to the PowerShell and execute the script.

Below is the script which need to be run.

######Importing the Csv file##########
$DataSheet = Import-Csv -path “<path to file>\TagCreation\tagsnew.csv” #Give the path of input Csv file.

foreach($data in $DataSheet)
{
###fetching data from CSV###
$SubscriptionName = $data.SubscriptionName
$ResourceGroupName = $data.ResourceGroupName
$ServerName = $data.Hostname
$ResourceType = “Microsoft./virtualMachines”
Select-AzureRmSubscription -SubscriptionName $SubscriptionName | Out-Null #Selecting the azure subscription
$ErrorActionPreference = “Stop”
######Creating/Updating tags for the server#######
try
{
$tags = @()
for($i=0;$i -lt 10;$i++)
{
$tagname = “tagname” + [string]$i
$tagvalue = “tagvalue” + [string]$i
if($data.$tagname)
{
if(($data.$tagname).trim() -ne “”)
{
$tags += @( @{ Name=$data.$tagname; Value=$data.$tagvalue })
}
}
}
$vm= Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $ServerName
Write-Verbose -Verbose “Adding tags to server $ServerName ”
Update-AzureRmVM -ResourceGroupName $ResourceGroupName -VM $vm <#-ResourceType $ResourceType#> -Tag $tags -ErrorAction Stop
}
catch
{
if($_.exception.message -like “*Long running operation failed with status*”){}
else
{
write-Host $_.Exception.Message -ForegroundColor Yellow
continue
}
}
}

Please download the input file here..

Leave a Reply

Your email address will not be published. Required fields are marked *

Bitnami