Highlighted
Contributor
Contributor

List all VMs without a tag

Jump to solution

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

0 Kudos
1 Solution

Accepted Solutions
Highlighted
User Moderator
User Moderator

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

0 Kudos
15 Replies
Highlighted
User Moderator
User Moderator

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

0 Kudos
Highlighted
Enthusiast
Enthusiast

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 vExpert 2018 -2020 | vExpert - NSX - Pro - Security | VCP6-DCV | VCP5.5-DCV | VCP5-DCV | VCA5-DCV | VCA5-Cloud | RHCSA & RHCE 6 | A+ _____________________________________ Please Mark "Helpful" or "Correct" if It'll help you _____________________________________ @Follow my Blog :- http://vmwarevtech.com/
0 Kudos
Highlighted
Contributor
Contributor

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.

0 Kudos
Highlighted
User Moderator
User Moderator

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

0 Kudos
Highlighted
Contributor
Contributor

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.

0 Kudos
Highlighted
Contributor
Contributor

$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.

0 Kudos
Highlighted
User Moderator
User Moderator

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

0 Kudos
Highlighted
Contributor
Contributor

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

0 Kudos
Highlighted
Contributor
Contributor

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.

😉

0 Kudos
Highlighted
User Moderator
User Moderator

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

0 Kudos
Highlighted
Contributor
Contributor

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)

0 Kudos
Highlighted
User Moderator
User Moderator

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

0 Kudos
Highlighted
Contributor
Contributor

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.

0 Kudos
Highlighted
User Moderator
User Moderator

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

0 Kudos
Highlighted
Contributor
Contributor

YES !!! it works !!!

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

0 Kudos