VMware Cloud Community
AllBlack
Expert
Expert
Jump to solution

Find VM without a tag in certain category

Hi there,

I am making use of tags so I can identify which VM are up for review. I have a category called "Expiry Date" and they contain tags named after the months.
I want to get a list of VM which do not have a tag assigned belonging to the "Expiry Date" Category.

The following returns my VM and associated tags

get-vm | select name,@{N="Tag";E={(get-tagassignment $_.name).tag.category}}

An example output would be as follows

Name          Tag

vm1

vm2          Expiry Date

vm3          Expiry Date

vm4

I'd like to retrieve just vm1 and vm4 as they have no tag associated but can't seem to make it work

I tried appending "where {$_.tag -eq $null}" but that gives me the exact same output

I also tried using the following command

get-vm | select name,@{N="Tag";E={(get-tagassignment $_.name).tag | where { $_.tag -notmatch "Expiry Date"}}}


This also returns all VM regardless whether they have tag value.


Any ideas as to what I am doing wrong?

Please consider marking my answer as "helpful" or "correct"
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this

$cat = Get-TagCategory -Name 'Expiry Date'

Get-VM |

Where{Get-TagAssignment -Entity $_ -Category $cat} |

Select Name,@{N="Tag";E={(Get-TagAssignment $_.name).tag}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

14 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

$cat = Get-TagCategory -Name 'Expiry Date'

Get-VM |

Where{Get-TagAssignment -Entity $_ -Category $cat} |

Select Name,@{N="Tag";E={(Get-TagAssignment $_.name).tag}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

AllBlack
Expert
Expert
Jump to solution

Bedankt Luc 🙂

This returned a list with VM having a tag in the category. Since I wanted a list of the VM that did not have a tag in that category I modified it to be

Where{!(Get-TagAssignment -Entity $_ -Category $cat)} | Select Name

Got what I want now

Cheers

Please consider marking my answer as "helpful" or "correct"
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Oops, sorry about that.

Glad you found the solution yourself.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
DanielKYCourts
Contributor
Contributor
Jump to solution

Can this script be modified to only include two specific clusters.  We use VMware tagging for backups and the goal is to find new server builds that do not have a category of protection, so we can confirm we are not missing any servers from backups.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Just proceed the Get-MV with a Get-Cluster

Get-Cluster -Name cluster1,cluster2 | Get-VM |


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

DanielKYCourts
Contributor
Contributor
Jump to solution

It seems to work with one cluster, but both of my clusters have spaces in the name, so it is giving me an error that it cannot find the second cluster.  I tried to add quotes but it is not working 

 

Get-Cluster -Name "cluster 1","cluster 2" | Get-VM |

I also tried 
Get-Cluster -Name 'cluster 1','cluster 2' | Get-VM |

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are you saying that this

Get-Cluster -Name 'cluster 1','cluster 2'

doesn't return 2 cluster objects?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

DanielKYCourts
Contributor
Contributor
Jump to solution

I was able to get it to work with quotes, but how can I output the results to an email, or file?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

For a file, you could use Export-Csv, for an email you could use Send-MailMessage


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
DanielKYCourts
Contributor
Contributor
Jump to solution

I was able to get it to work with quotes, but how can I output the results to an email, or file?

Reply
0 Kudos
DanielKYCourts
Contributor
Contributor
Jump to solution

I have tried to export but the file is blank.  It does not include the vm's without tags.

Reply
0 Kudos
DanielKYCourts
Contributor
Contributor
Jump to solution

When I output to text the file is blank

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I would need to see the code you are using to investigate


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Jord_
Contributor
Contributor
Jump to solution

7 years later, but for the next guy like me.

I've found this works and doesn't truncate for easy copying:

 

get-vm | ?{ (get-tagassignment $_ -Category 'Expiry Date') -eq $null} | Format-Table -Property Name -AutoSize

 

 

Reply
0 Kudos