VMware Cloud Community
CG21
Enthusiast
Enthusiast
Jump to solution

Get VMs with Tag Like and export csv just vm name

Hi,

I would like to export in csc the list of my vm who have a specific tag.

i can do that but the powercli command extract the vm with all properties,

so i would like just the name of my vms.

i 'm blocked with this powercli command :

Get-VM | where {(Get-TagAssignment -Entity $_ | Select -ExpandProperty Tag) -like 'tag-1*'} | Export-CSV C:\Scripts\result\vms-tag-1.csv

thank you for your help.

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

Get-VM | Get-TagAssignment |

where{$_.Tag -like 'tag-1*'} |

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

Export-CSV C:\Scripts\result\vms-tag-1.csv -NoTypeInformation


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

View solution in original post

10 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

Get-VM | Get-TagAssignment |

where{$_.Tag -like 'tag-1*'} |

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

Export-CSV C:\Scripts\result\vms-tag-1.csv -NoTypeInformation


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

CG21
Enthusiast
Enthusiast
Jump to solution

It's OK.

Thank you very much

Reply
0 Kudos
nicholas1982
Hot Shot
Hot Shot
Jump to solution

Hi Luc,

I have 10,000 VMs across 5 linked VCs, I connect using "all-linked" and I want to Get-VM with a particular tag, doing this method seems quiet slow as it has to check each VM and check for that tag, is there any faster way you could think of?

Nicholas
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you know the Tag Category, you could do

Get-TagAssignment -Category Categoryxyz


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

nicholas1982
Hot Shot
Hot Shot
Jump to solution

Hi Luc,

To my last question I figured out another way which works very quickly, takes less than 1 minute now where as before with the other code it was taking 30 minutes.

$VCs = ('vc1', 'vc2', 'vc3', 'vc4', 'vc5')

Foreach ($VC in $VCs) {

  

    Remove-Variable -Name Tag

    $Server = Connect-VIServer -Server ($VC + ".vsphere.local") -Credential $vcCreds

    Write-Host "Connecting to $VC"

    $Tag = Get-Tag -Name VM-TAG-NAME

    Get-VM -Tag $Tag

    Disconnect-VIServer * -Force -Confirm:$false

}

Nicholas
DomLap
Contributor
Contributor
Jump to solution

and If I would like to add the UsedSpace on this command ?


Thanks

LucD

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm not sure which of the snippets in this thread you are referring to, but via the Entity property, you can access all the VM properties.

Get-VM | Get-TagAssignment |

   where {$_.Tag -like 'tag-1*'} |

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

   Export-CSV C:\Scripts\result\vms-tag-1.csv -NoTypeInformation


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

GSchram
Contributor
Contributor
Jump to solution

This is by far the fastest way to accomplish the task. 15.5 seconds against 887 tagged vm's. As mentioned by Nicholas1982, processing Get-TagAssignment against Get-VM takes 30+ minutes for large VM environments. What the OP is essentially looking for is all in the Get-TagAssignment cmdlet vs the very robust Get-VM.

Reply
0 Kudos
GSchram
Contributor
Contributor
Jump to solution

GSchram_0-1640228423231.png

 

This is by far the fastest way to accomplish the task. 15.5 seconds against 887 tagged vm's. As mentioned by Nicholas1982, processing Get-TagAssignment against Get-VM takes 30+ minutes for large VM environments. What the OP is essentially looking for is all in the Get-TagAssignment cmdlet vs the very robust Get-VM.

BillG1970
Contributor
Contributor
Jump to solution

Works quickly and without issue. 

"Get-TagAssignment -Category "ENTER YOUR TAGS CATAGORY NAME" | Select @{N='VM';E={$_.Entity.Name}} | Export-CSV C:\Scripts\result\vms-TAG_NAME.csv -NoTypeInformation"

Reply
0 Kudos