CG21
Enthusiast
Enthusiast

Get VMs with Tag Like and export csv just vm name

Jump to solution

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

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

7 Replies
LucD
Leadership
Leadership

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

CG21
Enthusiast
Enthusiast

It's OK.

Thank you very much

0 Kudos
nicholas1982
Hot Shot
Hot Shot

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 VCP6
0 Kudos
LucD
Leadership
Leadership

If you know the Tag Category, you could do

Get-TagAssignment -Category Categoryxyz


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

0 Kudos
nicholas1982
Hot Shot
Hot Shot

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 VCP6
DomLap
Contributor
Contributor

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


Thanks

LucD

0 Kudos
LucD
Leadership
Leadership

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