AllBlack
Expert
Expert

Find VM without a tag in certain category

Jump to solution

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

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

13 Replies
LucD
Leadership
Leadership

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

AllBlack
Expert
Expert

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"
0 Kudos
LucD
Leadership
Leadership

Oops, sorry about that.

Glad you found the solution yourself.


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

0 Kudos
DanielKYCourts
Contributor
Contributor

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.

0 Kudos
LucD
Leadership
Leadership

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

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 |

 

0 Kudos
LucD
Leadership
Leadership

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

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

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
DanielKYCourts
Contributor
Contributor

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

0 Kudos
DanielKYCourts
Contributor
Contributor

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

0 Kudos
DanielKYCourts
Contributor
Contributor

When I output to text the file is blank

0 Kudos
LucD
Leadership
Leadership

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


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

0 Kudos