VMware Cloud Community
Zciklacekic
Contributor
Contributor
Jump to solution

Remove-TagAssignment command removes all tags from Entity.

Say we have 5 tags in an Entity (Like a Virtual machine)

     Category1     TagName1

     Category2     TagName2

     Category3     TagName3

     Category4     TagName4

     Category5     TagName5

And you want to remove TagName5 with "TagNameX"

You cannot just set it with New-TagAssignment command becuase New-TagAssignment command sets the single cardinality Tag if there is no Tags already set on the entity. So you have to remove the tag first then Set it with New-TagAssignment command. But Remove-TagAssignment command removes all tags not a single tag. And when you do it on multiple VM's with a script it takes too much time to remove and set each of them. Is tere a way to Remove single Tag from an entity without removing all of them.

1 Solution

Accepted Solutions
Wh33ly
Hot Shot
Hot Shot
Jump to solution

Something like this ?

Get-TagAssignment -Category Category5 |Remove-TagAssignment

View solution in original post

0 Kudos
3 Replies
Wh33ly
Hot Shot
Hot Shot
Jump to solution

Something like this ?

Get-TagAssignment -Category Category5 |Remove-TagAssignment

0 Kudos
Zciklacekic
Contributor
Contributor
Jump to solution

You are smart. Thanks.

NelsonCandela
Enthusiast
Enthusiast
Jump to solution

For the record, especially and also for my personal purpose, I'd like to extend this question and answer a bit.

This is because I'm not a real programmer so I always need some time to figure out how some things work Smiley Wink and as I couldn't figure out directly how this works here's what I would like to add in terms of explanation.

Let's say there is a Tag Category that is called Operating System.

Linked to that category are a few tags like:

  • Windows 7 (64-bit)
  • Windows 8.1 (64-bit)
  • Windows 10 (64-bit)
  • Windows Server 2012 (64-bit)
  • Windows Server 2012 R2 (64-bit)

Also there are (let's say) five VMs that use that Category and link the Operating System Tag according to the installed OS on this VM.

As user Zciklacekic pointed out, the method Remove-TagAssignment by default removes all Tags from a VM rather than a specific one. But if there are other Categories and other Tags related to a VM we of course do not want that.

A statement to remove a (single*) Tag from a single VM would then look like this:

$vm = "Test-VM-Win10"

Get-VM $vm | Get-TagAssignment -Category "Operating System" | Remove-TagAssignment -Confirm:$false

As you can guess this VM has the Tag "Windows 10 (64-bit)" attached to it.

So it gets the VM, reads the Tag(s) that are associated with the Category "Operating System" for this VM and removes all Tags by deleting the reference to the Tag Category.

* Usually an Operating System Category should only be applicable on a VM object and shouldn't have multiple cardinality. It wouldn't make any sense to attach this Tag to a vDS or a ESXi host, would it?! So in case you have other objects with multiple cardinality the above statements removes all assigned Tags with that Category for the selected object or VM.

I'm currently working on an issue with those Tags as I often find VMs that - according to the .vmx configuration file - seem to have an Operating System installed that is not factually installed. So for example the configuration file says "Microsoft Windows Server 2012 (64-bit)" but the VMware Tools report "Microsoft Windows Server 2008 R2 (64-bit)". As this would cause issues when tagging a VM correctly I want to set the Tag according to the VMware Tools information rather than the .vmx configuration file information.

So I'll put the information in a CSV file and do the re-tagging.

As I cannot simply re-tag a VM by applying a new Tag of that same Category I'll precautionary remove the current assiged Tag and set a new Tag afterwards.

So also a big Thanks! from my side, too. And sorry for the elaboration above but I wanted to make it as clear as possible as I stumled upon this issue several times now and also have removed all Tags once which I hope will never happen again 😉 this was a productive environment. Lucky me I had a backup file with the Object <-> Tag info.

0 Kudos