1 2 Previous Next 15 Replies Latest reply on Feb 3, 2020 7:06 AM by staprinik

    List all VMs without a tag

    staprinik Novice

      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

        • 1. Re: List all VMs without a tag
          LucD Guru
          Community WarriorsUser ModeratorsvExpert

          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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: List all VMs without a tag
            sarikrizvi 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.

            • 3. Re: List all VMs without a tag
              staprinik Novice

              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
                LucD Guru
                User ModeratorsCommunity WarriorsvExpert

                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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                • 5. Re: List all VMs without a tag
                  staprinik Novice

                  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
                    staprinik Novice

                    $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
                      LucD Guru
                      User ModeratorsvExpertCommunity Warriors

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

                       

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

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

                      Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                      • 8. Re: List all VMs without a tag
                        staprinik Novice

                        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

                        • 9. Re: List all VMs without a tag
                          staprinik Novice

                           

                          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.

                           

                          ;-)

                          • 10. Re: List all VMs without a tag
                            LucD Guru
                            vExpertUser ModeratorsCommunity Warriors

                            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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                            • 11. Re: List all VMs without a tag
                              staprinik Novice

                              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)

                              • 12. Re: List all VMs without a tag
                                LucD Guru
                                Community WarriorsUser ModeratorsvExpert

                                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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                                • 13. Re: List all VMs without a tag
                                  staprinik Novice

                                  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.

                                  • 14. Re: List all VMs without a tag
                                    LucD Guru
                                    vExpertUser ModeratorsCommunity Warriors

                                    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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                                    1 2 Previous Next