Identify VMs without a tag (Powershell, PowerCLI)

Tags are a really helpful vSphere feature. They enable you to add valuable information to the inventory objects and make them searchable and sortable.

Some backup solutions like Veeam offer the option to use tags as selection criteria to define dynamic backup jobs. If you make use of this possibility you need sooner or later a way to identify non-tagged VMs. Because if you miss tagging a VM, in the worst case you do not have a backup…

Powershell/PowerCLI is a great way to deal with this challenge.

You can run a script before the backup job starts identifying non-tagged VMs and assign them a temporary tag to add them to a dynamic backup job. Additionally, you can send an email with the contained VMs to be reminded to take care of them.

So let’s start with the command to display all non-tagged VMs:

$VMnoTag = Get-VM | ?{(Get-TagAssignment $_) -eq $null}

identify non-tagged VMs

Assign a tag to all non-tagged VMs identified in the step above:

Now we know all non-tagged VMs in our environment. But how can we assign a tag for these VMs to eg. add them to a dynamic backup job?  The following command assigns the tag “nonTaggedVM”:

$VMnoTag | foreach {

New-TagAssignment -Tag nonTaggedVM -Entity $_

}

Send an email listing the non-tagged VMs:

The last step in this short example is to send an email to the administrator. The short message includes all non-tagged VMs for further action. All you have to do is to insert valid addresses and an SMTP server:

$body = “No tag was assigned to the following VMs: – $VMnoTag”

Send-MailMessage -to “admin@company.com” -from “script@company.com -Subject “Missing Tag” -body $body -SmtpServer smtp.company.com

 

If you want to learn more about tags you may be interested in my three-part blog post “Fun with Tags”:

Fun with Tags, Episode 1: Basics and PowerCLI
Fun with Tags, Episode 2: Tags and Veeam
Fun with Tags, Episode 3: Tags and vRealize Operations Manager

Fun with Tags

Leave a Comment

Your email address will not be published. Required fields are marked *