VMware Cloud Community
rekha_sharma
Contributor
Contributor

How can i get the list of VMs with or without having tags assigned to them

This is the command i have been trying but not giving vms without tags

Get-VM | Get-TagAssignment
   where {($_.Tag -eq $null) -or ($_.Tag -ne $null)} |
   Select @{N='VM';E={$_.Entity.Name}},`
   @{N='TagName';E={$_.Tag}},`
   @{N="Cluster";E={Get-Cluster -VM $_.Entity.Name}},`
   @{N="ESX Host";E={Get-VMHost -VM $_.Entity.Name}}

0 Kudos
3 Replies
LucD
Leadership
Leadership

When there is no tag assigned to a VM, nothing will be returned and there will be no $_.Tag property.

You could do something like this

Get-VM -PipelineVariable vm | ForEach-Object -Process {

    $tags = Get-TagAssignment -Entity $vm


    If($tags -eq $null){

        '' | Select @{N='VM';E={$vm.Name}},

            @{N='TagName';E={}},

            @{N="Cluster";E={Get-Cluster -VM $vm}},

            @{N="ESX Host";E={Get-VMHost -VM $vm}}  

    }

    else{

        $tags | Select @{N='VM';E={$_.Entity.Name}},

            @{N='TagName';E={$_.Tag.Name}},

            @{N="Cluster";E={Get-Cluster -VM $vm}},

            @{N="ESX Host";E={Get-VMHost -VM $vm}}

    }

}


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

0 Kudos
rekha_sharma
Contributor
Contributor

Thanks alot LucD, now i am want to send the output of this in via email. Can you please help me with that.

0 Kudos
LucD
Leadership
Leadership

You can use the Send-MailMessage cmdlet for that.

There are plenty of examples available in this community.


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

0 Kudos