Automation

 View Only
Expand all | Collapse all

List all VMs without a tag

  • 1.  List all VMs without a tag

    Posted Feb 03, 2020 10:59 AM

    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



  • 2.  RE: List all VMs without a tag

    Posted Feb 03, 2020 11:42 AM

    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)"}



  • 3.  RE: List all VMs without a tag

    Posted Feb 03, 2020 01:15 PM

    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.



  • 4.  RE: List all VMs without a tag

    Posted Feb 03, 2020 01:19 PM

    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()}



  • 5.  RE: List all VMs without a tag

    Posted Feb 03, 2020 01:29 PM

    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.



  • 6.  RE: List all VMs without a tag

    Posted Feb 03, 2020 01:46 PM

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



  • 7.  RE: List all VMs without a tag

    Posted Feb 03, 2020 02:05 PM

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

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

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



  • 8.  RE: List all VMs without a tag

    Posted Feb 03, 2020 02:10 PM

    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.

    ;-)



  • 9.  RE: List all VMs without a tag

    Posted Feb 03, 2020 02:27 PM

    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

        }

    }



  • 10.  RE: List all VMs without a tag

    Posted Feb 03, 2020 02:41 PM

    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)



  • 11.  RE: List all VMs without a tag

    Posted Feb 03, 2020 02:45 PM

    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.



  • 12.  RE: List all VMs without a tag

    Posted Feb 03, 2020 02:49 PM

    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.



  • 13.  RE: List all VMs without a tag
    Best Answer

    Posted Feb 03, 2020 03:02 PM

    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

            }

        }

    }



  • 14.  RE: List all VMs without a tag

    Posted Feb 03, 2020 03:06 PM

    YES !!! it works !!!

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



  • 15.  RE: List all VMs without a tag

    Posted Feb 03, 2020 02:07 PM

    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



  • 16.  RE: List all VMs without a tag

    Posted Feb 03, 2020 12:39 PM

    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.