16

Quick Tip - Update a Tag on an Azure Resource

 4 years ago
source link: https://thomasrayner.ca/quick-tip-update-a-tag-on-an-azure-resource/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Quick Tip - Update a Tag on an Azure Resource

Working with Azure resources can be a bit of an adventure sometimes. Say you want to update a tag on an Azure resource. Not remove it, but change its value. If you try to add a tag with the same name but different value, you’ll get an error that the tag already exists. Some of the ways you have available to get rid of a tag involve dropping all the other tags assigned to a resource. So, what do you do?

In this example, I have a couple VMs with a tag named “user” and a value of “thmsrynr”, and I want to keep the tag but change the value to “Thomas”.

Well, this extravagant one-liner will do the trick.

Find-AzureRmResource -TagName user | 
    ForEach-Object { Get-AzureRmResource -ResourceId $_.ResourceId |
    ForEach-Object { $tags = $_.Tags; 
                     $tags['user'] = 'Thomas'; 
                     Set-AzureRmResource -Tag $tags -ResourceId $_.ResourceId } }

I like Find-AzureRmResource best for searching for resources with a specific tag, but it doesn’t return the tags for some reason that is beyond me. You can search by tag but the tags aren’t returned? Weird, right?

Anyway, I pipe everything I find into Get-AzureRmResource which is bad at searching for resources but DOES return the tags. Then for the resource I find, I store the tags on that resource in a temporary variable (named $tags), and then I work with the variable instead of working directly with the Azure object to update the “user” tag I care about. Then I set the tags on that resource to be what I stored. This should keep all the other tags intact, and update the value of one specific tag.

You could, and probably should, expand this into a more flexible function with parameters and filtering and such, but this example shows you how the tricky bits work.

Written on March 7, 2018

Recommend

  • 21

    Quick Tip: Simulate a bad network connection on macOS (and Linux)2020-06-19Yesterday, I got a tip from a colleague to fake a poor network connection (for debugging an issue) with tc (

  • 10

    Quick Tip: GitHub Action for pushing docker images2020-06-16Here is a full example of using the official Docker build-push-action for GitHub to publish directly to th...

  • 14

    Quick Tip - See All The Tab-Completion Options At Once In The PowerShell Console If you’re used to working in VS Code or the PowerShell ISE, you’ve undoubtedly enjoyed intellisense which is the feature that shows you all...

  • 20

    Quick Tip - Split A PowerShell Collection Into Two Arrays Did you know that you can use Where-Object to split a collection into two arrays? Like, if you had an array containing the numbers 1 to 10, you c...

  • 13
    • thomasrayner.ca 4 years ago
    • Cache

    Quick Tip - Re-Run The Last Command

    Quick Tip - Re-Run The Last Command Sometimes, while you’re poking around in the console, you want to re-run the last command. Sure, you can hit the up arrow and enter, but PowerShell always gives you multiple ways to do...

  • 7

    Quick Tip - PowerShell Supports Partial Parameter Names Did you know that PowerShell supports the usage of partial parameter names? This isn’t such a big deal since tab completion is a thing… and if you’re writing code,...

  • 17

    Quick Tip - Open A File In Default Program When you double click a file in Explorer.exe, it automatically opens in its default program if it has one associated with its type. But did you know you can do the same thing us...

  • 10

    Quick Tip - Did the last command work or not? In PowerShell, there is usually at least a few ways to do most tasks and detecting if the last command resulted in an error or if it worked is no exception. You could wrap co...

  • 15

    Quick Tip - Use PowerShell To See How Many Files Are In A Directory Here’s a way to see how many files are in a directory, using PowerShell. As you likely know, you can use Get-ChildItem to get...

  • 10

    Quick Tip - Diagnosing Slow PowerShell Load Times I could write an entire book on “why does my PowerShell console take so long to load?” but I don’t want to write that book. Instead, here’s a way to make sure the reason...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK