i need to set specific tags key value

Mohamed CHALABI 1 Reputation point
2022-02-22T15:16:33.183+00:00

I am trying to pull some tags through azure ressourec

tag decom

fist i need to get the value from the tag decom
second i need to increase the value +1 and set in in decom tag

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,763 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. shiva patpi 13,251 Reputation points Microsoft Employee
    2022-02-22T22:25:52.08+00:00

    Hello @Mohamed CHALABI ,
    You can do it using powershell script , below are the commands:

    $tags = (Get-AzResource -ResourceGroupName ResourceGroupName -Name VirtualMachineName).Tags //Get the tags on that Virtual Machine
    $tags["decom"] = [int]$tags["decom"] + 1 //Increase the value of decom tag
    Set-AzResource -ResourceGroupName ResourceGroupName -Name VirtualMachineName -ResourceType "Microsoft.Compute/VirtualMachines" -Tag $tags //set the updated tag

    Before execution of the script:

    176890-image.png

    Step By Step Execution of above script:

    Step1:- Get the tags
    $tags = (Get-AzResource -ResourceGroupName ResourceGroupName -Name VirtualMachineName).Tags

    Step2:- Check the current tag decom value:
    $tags

    Key Value
    --- -----
    decom 1

    Step3:- Increase the tag decom value

    $tags["decom"] = [int]$tags["decom"] + 1

    Step4:- Check the increased tag value

    $tags

    Key Value
    --- -----
    decom 2

    Step5:- Update the VM with new tag value

    Set-AzResource -ResourceGroupName ResourceGroupName -Name ResourceGroupName -ResourceType "Microsoft.Compute/VirtualMachines" -Tag $tags

    177001-image.png

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.