With the new 5.5 R2 release of PowerCLI we introduced end-to-end support for the vSphere Tagging functionality. You can now automate Tag and TagCategory management, assign tags on objects and search for objects by tag. This blog post will give you an overview of the new functionalities and a couple of useful scripts:
- One for exporting/importing of tags
- One for converting the legacy custom attributes to tags
Managing Tag Categories
Tag Categories allow you to group related tags together. When you define a category, you can also specify which object types its tags can be applied to and whether more than one tag in the category can be applied to an object. Creating a category with PowerCLI is easy:
New-TagCategory –Name [name] -Description [description] -Cardinality [single/multiple] –EntityType [list of types]
For example let’s create a category that will contain a tag for each vSphere user. We’ll name this category “Owner” and use it to tag the owners of Virtual Machines. The tags will only be applicable to Virtual Machines and a Virtual Machine can have only one owner. The following script creates such a category:
New-TagCategory –Name “Owner” –Cardinality single –EntityType VirtualMachine
The “-Cardinality” parameter specifies whether a single or multiple tags from this category can be applied to the same object at the same time. If you don’t specify it the default is “single”. The “-EntityType” parameter allows you to specify the object types to which you can attach tags from this category. If you omit the parameter by default the category will be applicable to all supported entity types.
The full list of supported entity types is: VirtualMachine, VM, VMHost, Folder, Datastore, DatastoreCluster, Cluster, ResourcePool, DistributedSwitch, DistributedPortGroup, VirtualPortGroup, VApp, Datacenter, All.
Modifying an existing TagCategory is done through Set-TagCategory cmdlet. You can change its name, description, cardinality (you can only extend it to “multiple”, restricting it to “single” is not possible) and add more entity types (again you can only extend the applicable entity types).
In our previous example the “Owner” category is only applicable to Virtual Machines, however VApps can also have an owner. Let’s modify the category to allow tagging VApps as well:
Get-TagCategory “Owner” | Set-TagCategory –AddEntityType VApp
Removing an existing category is just as easy by using the Remove-TagCategory cmdlet. Note that by removing a category you are also removing all tags in it and any assignments of these tags! Here is an example:
Remove-TagCategory “Owner”
Managing Tags
Once you have a tag category you are able to create new tags in it. This is done through the New-Tag cmdlet. For example in our “Owner” category let’s create a tag for John Smith:
New-Tag –Name “jsmith” –Category “Owner”
You can also create multiple tags – by reading the input values from CSV file or from other cmdlets. Here is how to create tags for each user in the “Example.org” domain:
# Retrieve all user accounts in the “Example.org” domain
$userList = Get-VIAccount –User –Domain “Example.org”
# For each user account create a new tag based on the user’s Id
foreach ($user in $userList) { New-Tag –Category “Owner” –Name $user.Id –Description $user.Description }
Modifying an existing tag is done through the Set-Tag cmdlet. It allows you to change the tag’s name and description:
Get-Tag “jsmith” –Category “Owner” | Set-Tag –Description “John Smith”
Removing a tag is done through Remove-Tag cmdlet, similar to removing a category. When removing a tag you will automatically remove any assignments of this tag.
Now that you have created some tags – it’s time to put them to use. You can assign tag to an entity using the New-TagAssignment cmdlet. Here is how to assign the “jsmith” tag to the VMs that belong to John (we assume they have “jsmith” in their name):
Get-VM –Name *jsmith* | New-TagAssignment –Tag “jsmith”
You can easily retrieve all tag assignments on a given entity:
Get-VM jsmith_vm1 | Get-TagAssignment
Or you can retrieve all VMs that have a given tag associated with them:
Get-VM –Tag “jsmith”
If you want to build a report that displays information about your inventory objects and all tags associated with them – you can do that by using the New-VIProperty cmdlet to add a “Tag” property to your objects. Here is an example of how to do that for the VirtualMachines, but it is easily applicable to other types as well:
# First we want to define the new “Tag” property of the VirtualMachine object
New-VIProperty -Name Tag -ObjectType VirtualMachine -Value `
{ Get-TagAssignment -Entity $args[0] | select -ExpandProperty Tag }
# Now retrieve all VMs and their tags:
Get-VM | select Name, Tag
Exporting and importing tags
Now that you know the basics, let’s do something more advanced. If you have created all your tags with a script using the above commands it’s very easy to run this scrip on another vCenter Server in order to replicate all tags on that server as well. But if you created them by hand how can you replicate them on a different VC? Well, here is one solution, using the attached script “ExportImportTags.ps1” you will be able to export your tag configuration from one VC and then import it to another (one or multiple).
To use the script – simply download it from here and save it in a folder of your preference. Start PowerCLI and load the script:
. <path_to_folder>\ExportImportTags.ps1
Then connect to the VC you want to export tags from:
$sourceVC = Connect-VIServer <connection_parameters>
To export all tags use the Export-Tags function. It accepts two parameters, the server from which to export the tags and a destination file where to save them:
Export-Tags –Server $sourceVC –Destination C:\vc1_tags.txt
Then connect to the VC(s) that you want to import those tags to:
$destinationVC = Connect-VIServer <connection_parameters>
To import the tags use the Import-Tags function. It accepts two parameters, the server on which to import the tags and a source file from which to read them (the file produced by Export-Tags earlier):
Import-Tags –Server $destinationVC –Source C:\vc1_tags.txt
That’s it! You now have the same tag configuration across all your VCs.
Converting your existing custom attributes to tags
What if you are using the legacy custom attributes and want to convert those to tags? The attached “ConvertCustomAttributesToTags.ps1” script will do that for you. The script will scan your existing custom attributes and annotation values and based on those create the corresponding tag categories and tags. It will then assign the newly created tags to your inventory items. Using the script is done in the same way as above, download it from here, start PowerCLI, load the script and connect to your vCenter server:
. <path_to_folder>\ ConvertCustomAttributesToTags.ps1
$sourceVC = Connect-VIServer <connection_parameters>
To run the script just call the ConvertCustomAttributesToTags function and pass it the server you want to operate on:
ConvertCustomAttributesToTags $server
Once the function completes you will have tags matching your existing custom attribute hierarchy. If you like you can remove the old custom attributes.
This post was created by Dimitar Barfonchovski.Dimitar joined VMware and the PowerCLI team in 2007. He is member of the development part of the team and his main responsibilities are the functional design and implementation of features for the vSphere and vCloud PowerCLI components.
As with all members of the team, he is working to deliver a good and valuable product. He is also working to improve all processes and tools involved in the product development and validation.
Nicely done Dimitar!
I am unable to get this to work. I am very new to powerCLI and to CLI in general. I run the script, connect to the server, I don’t know what to do next as Export-Tags isn’t recognized. I am just trying to export the tags from one domain’s vcenter to another domain’s vcenter.
Any assistance would be great.
There is a difference between the blog post and the ExportImportTags.ps1 script; the script uses Export-Tag and Import-Tag while the blog post mentions Export-Tags and Import-Tags. Notice the extra “s”
this simply didn’t work at all. I used the Export-Tag (no ‘S’) and nothing happened. no error, but no result.
unblock the script
open PowerCLI 5.5
set scripts to unrestricted
navigate to path of extracted script
run script – \ExportImportTags.ps1
connect to Server storing in var – $sourceVC = Connect-VIServer
run Export-Tag function with 2 params. – Export-Tag –Server $sourceVC –Destination C:\vc1_tags.txt == nothing.
I have full admin rights within vsphere, local system running the script and on VC Server..
I’ve never met a PS Script that worked as posted, that streak continues here….
Rename ExportImportTags.ps1 to ExportImportTags.psm1
Run “Import module ExportImportTags.psm1”
$sourceVC = Connect-VIServer
Export-Tag –Server $sourceVC –Destination vc1_tags.txt
$destinationVC = Connect-VIServer
Import-Tags –Server $destinationVC –Source vc1_tags.txt
Awesome piece of content.
Yeah nicely done Alan Renouf the script is working fine as of now 🙂
Is there a way when running the report for VM’s and their tags to put a comma between each tag to make the report more readable?
Besides that, awesome article. We are finally getting around to 5.5 and this has helped implement tagging in our environment.
Really Amazing Script.This is very helpful for me
Hi Dimitar,
I saw view objects have Tag property but it is empty no matter tag is assigned to object or not. Is it just reserved for future use or it’s a bug?
Is a tag assignment assigned to an inventory id? If I remove a Vm from the inventory and then readd it will it lose its tag assignments?
For latest news you have to pay a quick visit internet and on internet I found this
website as a best web page for latest updates.
I have a question. If there’s a way to export tags, why isn’t there to properly back them up? I just had to re-install my VCenter and lost all tags; I called support and they’re saying that I have to manually type them in – even with a backup/snapshot of my VCenter server these can’t be restored. This makes no sense, and it’s poor thinking from VMware.
Follow below sequence to utilize this feature:
Rename ExportImportTags.ps1 to ExportImportTags.psm1
Run “Import module ExportImportTags.psm1”
$sourceVC = Connect-viserver vcenterservername -user domain\user -pass ad343a#
Export-Tag –Server $sourceVC –Destination vc1_tags.txt
$destinationVC = Connect-viserver vcenterservername -user domain\user -pass ad343a#
Import-Tag –Server $destinationVC –Source vc1_tags.txt
Is this supposed to be ran line by line. I still get an error. This is what I am doing.
1. Extracted ExportImportTags.ps1 from downloaded zip. I then renamed the file to ExportImportTags.psm1
2. Launch PowerCLI 5.5 (Release 2 Patch 1)
3. “Import module ExportImportTags.psm1” no output
4. $sourceVC = Connect-viserver vcenterservername -user domain\user -pass ad343a# I changed the credentials and vcenterservername
5. Export-Tag –Server $sourceVC –Destination V:\tags\vc1_tags.txt
I still get the following error.
Export-Tag : The term ‘Export-Tag’ is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Export-Tag -Server $sourceVC -Destination v:\tags\vc1_tags.txt
+ ~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Export-Tag:String) [], CommandN
otFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
I am not sure what else I should try?
Hi to every one, the contents existing at this website
are in fact awesome for people knowledge, well, keep up the
nice work fellows.
I am tagging my environemnts now. the isue i am facing is that I need the tag name to be same in both Development and Production environemnt.however they will be tagged to different entities.
but when i run the New-tagassignment cmdlet it throws an error which says that there are 2 tags with the same name .
how do i resolve this error.
The script uses Export-Tag and Import-Tag while the blog post mentions Export-Tags and Import-Tags.
get it. thanks
Joe Coulombe Biography
get it. thanks. awesome. really good script. thanks man. really good
Joe Coulombe Biography
Spectrum Solution is one of the reputed Web Development Company in Qatar. The work of the web developer team is to develop effective websites which add values to the business of its customer. The prime importance of the team is flexibility, functionality, and usability. It also helps to build the basis of the website’s design and architecture.
Very Nice an Amazing Website
Very Nice and an Amazing blog
Camille Kostek Biography
All Celebrity bio and more
Thanks Buddy for this useful Information.
Bigstar Bio is your Biography, entertainment, music & biodata website. We provide you with the latest news and videos straight from the entertainment industry
Thank you so much for this amazing information. You may also check out more biographies and net worth.
We are a Best Digital Marketing Company in Noida, Delhi with an aspiration to create value for you. Our team of professional digital marketing consultants has been creating value for our clients in diverse industries. We think that it is not about what services we provide but about how we offer it. Our approach to digital business makes us one of the top 10 digital marketing companies in India. We provide support in the following areas
celebritiesdata is your Biography, biodata website. We provide you with the latest news and gossips straight from the entertainment industry.
Find out more about celebrities like Jordyn Woods and many more.
TheDuggu is an Entertainment platform where you can find updates about songs, Bollywood news, actor’s biography. Here we will update the news, new upcoming songs, and movies. Also, we will publish about the movies and ratings of it according to the public reviews. TheDuggu.com
Hi thanks for the post Biography
Wow great post check this
Thanks Buddy for this useful Information
Finally got the working script! Thanks for sharing
Enjoyed reading the article above , really explains everything in detail, the article is very interesting and effective.
Very informative and useful content thanku for sharing your knowledge
Thanks you for useful information
Lovely just what I was looking for. Thanks to the author for taking his time on this one.
I?m impressed, I must say. Rarely do I come across a blog that?s both equally educative and entertaining, and let me tell you, you have hit the nail on the head.
The problem is an issue that too few men and women are speaking intelligently about.
Now i’m very happy I stumbled across this during my hunt for something regarding this.
Nick Chavez is a famous hairdresser from America. He is a well-known figure throughout the United States for his work; he belongs to Arizona Likewise.
For those who want a quick look at the content of this blog post,
Creating a category with PowerCLI is easy:
New-TagCategory –Name [name] -Description [description] -Cardinality [single/multiple] –EntityType [list of types]
Nice Post for valuable information
RBM Creative Media
Nice Post for valuable information
RBM Creative Media
nice post very good information
RBM Creative Media
thanks for good infromation
Thank you for the useful information
Loved The Way You Described It!
Vshphere tags are kind of useful! thanks for the shared.
Great information! I love it vmware
celebritytags.com Based On Explore Real Net Worth of Actors, Actresses, Athletes, Politicians, Businessmen, Cricketer, Footballer, Writer, Blogger and other famous celebrities
How Are You? .Bruno Mars Height I would like to take the ability to say thanks to you for the professional suggestions I have usually enjoyed browsing your site. Excellent post. Known for her beautiful voice and attractive acting. I love to read this blog because i know this is really helpful in all fields.
Nice Post for valuable information
It is valuable and informative content.
finally got the most understanding content on the web!
The author of this article and post share the fabulous and tremendous work that need to applaud as once, as a community we help each other, and let’s not forget that we are not alone in this fabulous community or committee. networthpower
is website that publish content on celebrities’ net worth and latest and trending news topics on popular peoples.
Your site is very informative and fantastic information, it has helped me a lot. thank you!
I was struggling with this issue for a week. Genuinely I want to say thank you.
To begin, unplug all cables from the Dell laptop. This includes anything that’s plugged into its USB port or power adapter for charging — including cell phones — you definitely don’t want them draining any extra juice out of there.
Next, remove any external drives so they don’t drain your laptop’s battery if they haven’t been used recently.
This blog is really big and great source for information, Thanks a lot of you for giving us suitable information by your awesome post.
Nice Post for valuable information
hola, muy buena tu escritura, tengo conocimiento que has escrito en tu articulo, es de mucha ayuda, gracias amigo.
I like what has been explained in your article, it really inspires me. Thank you.
when looking for an article that is comfortable to read in the afternoon if I say this page is the right one, I am happy to read today in my afternoon. Thank you.
when looking for an article that is comfortable to read in the afternoon if I say this page is the right one, I am happy to read today in my afternoon. Thank you.
Allow me to share my link here, before I thank you in advance.
The post I was looking for! I am so happy to finally read this post. Thank you very much. Can I refer to your post on my website? Your post really touched me and helped me a lot. If you have any questions please visit my site and read what kind of posts I post. I’m sure it will be interesting.
I am happy with what has been revealed in this article, I am always looking for good articles like the one I have found. very helpful. thank you for your content.
Agen slot online terbesar dan tergacor di tahun ini 2023
yaitu situs slot online komandan88
You can find reliable t shirt plastic bag suppliers on Trade Key, a leading B2B marketplace. Trade Key will connect you with suppliers who can provide you with high quality t shirt plastic bags for your store.
Your site is very informative and has fantastic information, it has helped me a lot. thank you!
Hi,
Really happy to say your post is very interesting to read. I never stop myself to say anything about it. We are the Certified and Best Manpower Agency in Qatar, providing Manpower supply in different industrial segments. Check our page, “https://www.matrixintl.org/index.htm”. Expecting more blogs.
so much positive aura right here , im really appreciated what you doing , anyway please visit m website on TOTAL138
Hi,
Really happy to say your post is very interesting to read. I never stop myself to say anything about it. We are the most trusted and fast-growing manpower supply company in Qatar. Check our page, “https://www.matrixintl.org/index.htm”. Expecting more blogs.
Matrix Incorporated Contracting Company W.L.L. (MICC) and has earned a reputation as one of the best oil and gas manpower supply companies in Qatar, and we sift through the candidate applications strictly.
thank you for nice information ; UMJ Modern