none
Deleting a specified tag from a single resource using Powershell RRS feed

  • Question

  • Hi all,

    Powershell amateur here - go easy on me!

    I am currently trying to write a script to assist with the automation of starting up and stopping VM's. While the Microsoft Start Stop utility works to an extent, its not flexible enough for what we need.

    I am creating a script which launches a powershell form, and the user can then graphically select the hours a VM is running on a daily, weekday, weekendly basis. This will be utilising tagging.

    For this example, this will be tagging a VM to start and stop daily using the "Start_Daily" and "Stop_Daily" tags:

    $VMName = "vmstartstoptest"
    $RGName = (Get-AzVM -Name $VMName).ResourceGroupName
    $StartTime = Read-Host "Enter Start Time: "
    $StopTime = Read-Host "Enter Stop Time: "

    $tags = (Get-AzResource -ResourceGroupName $RGName -Name $VMName).Tags
    $tags.Add("Daily_Start","$StartTime")
    $tags.Add("Daily_Stop", "$StopTime")
       
    Set-AzResource -ResourceGroupName $RGName `
                   -ResourceName $VMName `
                   -ResourceType Microsoft.Compute/virtualMachines `
                   -Tag $tags `
                   -Force

    Now this code works fine for adding the tags initially without removing any other tags from vmstartstoptest.

    The problem I face is if that VM already has those tags in place (Daily_Start : 0900 for example), the above code will error as it states that there is a tag already in place with that name.

    I've tried to use the following code to loop through the tags on that VM with a view to do something with the tag, however due to having to use $tags.keys to filter Daily_Start, it only returns a string. If I used foreach ($tag in $tags), it ignores the filter logic and returns all keys on that VM if it finds a match for one of them:

    foreach ($tag in $tags.Keys)
        { if ($tag -eq "Daily_Start")

    Is there a way of deleting the Daily_Start tag from the VM without deleting all the tags, and without deleting that tag completely as it will be in use on other resources? I think it would be easier to delete the tag if it already exists and add again than doing an if statement to look up the existence of the tag, then update it if it already exists or create if it does.

    Thanks in advance :)


    Dave

    Tuesday, August 20, 2019 10:36 AM

Answers

  • Hi,

    I have simple sample that I think will work for you. It is located here:

    https://github.com/insidemscloud/InsideAzureMgmt/blob/master/Chapter10/EventGrid/UpdateVMTag.ps1

    It is part of the free Inside Azure Management book.

    Particularly this part checks if the tag is present at all in the subscription:

    # Check if tag name exists in subscription and create if needed.
            $TagExists = Get-AzureRmTag -Name $TagName -ErrorAction SilentlyContinue
            if ([string]::IsNullOrEmpty($TagExists))
            {
                New-AzureRmTag -Name $TagName | Out-Null
            }

    And this part removes the existing tag and removes it. If the tag is not already present it will just continue to adding the tag:

    # Check if this VM already has the tag set.
            $vm = Get-AzureRmVM -ResourceGroupName $vmResourceGroup -Name $vmName
            $vm.Tags.Remove($tagName) | Out-Null
            $tag = @{"$tagName"=$tagValue }
            $allTags = $vm.Tags + $tag
            # Add tag to VM
            Write-Output "Adding LastModifiedDate tag to VM $($vm.Name) in resource group $vmResourceGroup."
            Update-AzureRmVM -ResourceGroupName $vmResourceGroup -VM $vm -Tag $allTags | Out-Null

    If that helps you please mark this reply as answer.

    Wednesday, August 21, 2019 5:33 AM

All replies

  • Hi,

    I have simple sample that I think will work for you. It is located here:

    https://github.com/insidemscloud/InsideAzureMgmt/blob/master/Chapter10/EventGrid/UpdateVMTag.ps1

    It is part of the free Inside Azure Management book.

    Particularly this part checks if the tag is present at all in the subscription:

    # Check if tag name exists in subscription and create if needed.
            $TagExists = Get-AzureRmTag -Name $TagName -ErrorAction SilentlyContinue
            if ([string]::IsNullOrEmpty($TagExists))
            {
                New-AzureRmTag -Name $TagName | Out-Null
            }

    And this part removes the existing tag and removes it. If the tag is not already present it will just continue to adding the tag:

    # Check if this VM already has the tag set.
            $vm = Get-AzureRmVM -ResourceGroupName $vmResourceGroup -Name $vmName
            $vm.Tags.Remove($tagName) | Out-Null
            $tag = @{"$tagName"=$tagValue }
            $allTags = $vm.Tags + $tag
            # Add tag to VM
            Write-Output "Adding LastModifiedDate tag to VM $($vm.Name) in resource group $vmResourceGroup."
            Update-AzureRmVM -ResourceGroupName $vmResourceGroup -VM $vm -Tag $allTags | Out-Null

    If that helps you please mark this reply as answer.

    Wednesday, August 21, 2019 5:33 AM
  • Hi Stanislav,

    I've tested this now and it works a treat, thank you very much for your help :)

    Regards

    Dave

    Wednesday, August 21, 2019 10:20 AM