VMware Cloud Community
staprinik
Contributor
Contributor
Jump to solution

List all VMs without a tag

I'm struggling to figure out how to list the VMs that don't have a tag named "LINUX" or "WINDOWS" in "OS" category.

For now, i can find all VMs without a tag, like that :

Get-VM | Where-Object{(Get-TagAssignment $_) -eq $null)}

But, this result can be false because VMs can have another tag (not LINUX or WINDOWS).

Any idea is welcome.

Regards

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

A light bulb moment.
I was forgetting the ones without any tags of course.

$tgtCat = 'OS'

Get-VM -PipelineVariable vm |

ForEach-Object -Process {

    $tags = Get-TagAssignment -Entity $_

    if($tags -eq $null){$vm}

    else{

        if($tags.Tag.Category.Name -notcontains $tgtCat){

            $vm

        }

    }

}


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

View solution in original post

15 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this.

Note that this will only check VMs that have a tag in the OS category.

$tagCategory = 'OS'

$tagNames = 'LINUX','WINDOWS'


Get-TagAssignment -Category $tagCategory | where {$tagNames -notcontains "$($_.Tag.Name)"}


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

Reply
0 Kudos
sarikrizvi
Enthusiast
Enthusiast
Jump to solution

Use RVtool and get detail in excel and later on you can compare which VM has a Tag or not with filtering either it's false or not.

I didn't try with script so far , so not sure on that.

Regards,
SARIK (Infrastructure Architect)
vExpert 2018-2020 | vExpert - Pro | NSX | Security
vCAP-DCD 6.5 | vCP-DCV 5.0 | 5.5 | 6.0 | vCA-DCV 5 | vCA-Cloud 5 | RHCSA & RHCE 6 | A+ (HW & NW)
__________________
Please Mark "Helpful" or "Correct" if It'll help you
_____________________________________
@Follow:
Blog# https://vmwarevtech.com
vExpert# https://vexpert.vmware.com/directory/1997
Badge# https://www.youracclaim.com/users/sarik
Reply
0 Kudos
staprinik
Contributor
Contributor
Jump to solution

unfortunely, it doesn't work as expected.

No VM are displayed whereas, i'm sur that i've at least one with no tag.

To list all VMs that match the category "OS", i did that:

Get-TagAssignment -Category $tagCategory

and the result is that:

Uid : /VIServer=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Tag : OS/WINDOWS

Entity : PRD-webxxxxxxx

Id : com.vmware.cis.tagging.TagAssociationModel

Name : com.vmware.cis.tagging.TagAssociationModel

So, the mistake is probably in the "where-object" condition. But, i'm beginner, and don't know how to fix that.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Just to clarify, you also want to list the VMs that do not have a tag in the OS tag category assigned?
The above script only lists the VMs that have a tag in the OS tag category that is not LINUX or WINDOWS.

If that is the case, you probably want to run something like this

$SpecificTags = 'Cat1/Tag1','Cat1/Tag3'

Get-TagAssignment | where{$SpecificTags -notcontains $_.Tag.ToString()}


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

Reply
0 Kudos
staprinik
Contributor
Contributor
Jump to solution

sorry, i probably bad explained.

i want, all my VMs to be tagged with an OS tag (Linux or Windows).

So, to check, i want to list all VMs that don"t have OS tag.

But, this VMs can have another tag like backup.

To summ up:

CATEGORYTAG
OSLINUX
OSWINDOWS
BACKUPBACKUP-PRD
BACKUPBACKUP-OFF

regards.

Reply
0 Kudos
staprinik
Contributor
Contributor
Jump to solution

$SpecificTags = 'Cat1/Tag1','Cat1/Tag3'

Get-TagAssignment | where{$SpecificTags -notcontains $_.Tag.ToString()}

this script list all VMs that have another tag than OS category but it don"t say if the tag OS category is set or not.

i explain:

the VM PRD-001 has 2 tags:

  - OS/LINUX

  - BACKUP/BACKUP-PRD

the VM PRD-002 has only one tag:

  - BACKUP/BACKUP-PRD

your script will display PRD-001 and PRD-002 because, they have BACKUP/BACKUP-PRD tag.

i want a script that display only PRD-002 because no OS tag is assigned.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Those were sample tags, in your case it should probably be

$SpecificTags = 'OS/LINUX','OS/WINDOWS'

Get-TagAssignment | where{$SpecificTags -notcontains $_.Tag.ToString()}


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

Reply
0 Kudos
staprinik
Contributor
Contributor
Jump to solution

I want something like that :

foreach VM

  get tags of the VM

    foreach tag of tags

      get category of tag

        is this tag category == OS

          if yes, NEXT tag

          if not, display in the list

i hope, it will help

Reply
0 Kudos
staprinik
Contributor
Contributor
Jump to solution

Those were sample tags, in your case it should probably be

$SpecificTags = 'OS/LINUX','OS/WINDOWS'

Get-TagAssignment | where{$SpecificTags -notcontains $_.Tag.ToString()}

yes, thank you Luc, i'd well understood.

i had already replace samples tags by mines.

but i confirmed that it list tags not VM.

So, a VM that have a tag BACKUP/BACKUP-PRD will be displayed.

The goal is to display VMs that don't have OS/LINUX or OS/WINDOWS.

😉

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Something like this you mean?

$tgtCat = 'OS'

Get-TagAssignment |

Group-Object -Property {$_.Entity.Name}|

ForEach-Object -Process {

    if($_.Group.Tag.Category.Name -notcontains $tgtCat){

        $_.Name

    }

}


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

Reply
0 Kudos
staprinik
Contributor
Contributor
Jump to solution

we are close to the good answer i think.

when i run your script, it display nothing.

but, if i add an "else" like that :

if($_.Group.Tag.Category.Name -notcontains $tgtCat){

  $_.Name

}

else {

  $_.Name

}

all VMs are displayed expect the target one (the VM with no OS category tag)

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That can only mean that all your VMs have a tag in the OS tagcategory but not with the tags LINUX or WINDOWS.
Which you said is not the case.

I give up.


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

Reply
0 Kudos
staprinik
Contributor
Contributor
Jump to solution

sorry for consumed your time.

But, in the web GUI, i have a test VM with no tag at all.

So, i wanted a script that warn me of a VM "OS untagged".

Never mind.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

A light bulb moment.
I was forgetting the ones without any tags of course.

$tgtCat = 'OS'

Get-VM -PipelineVariable vm |

ForEach-Object -Process {

    $tags = Get-TagAssignment -Entity $_

    if($tags -eq $null){$vm}

    else{

        if($tags.Tag.Category.Name -notcontains $tgtCat){

            $vm

        }

    }

}


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

staprinik
Contributor
Contributor
Jump to solution

YES !!! it works !!!

thank you very much Luc for the time spent and your availability.

Reply
0 Kudos