Automation

 View Only
  • 1.  Get VMs with Tag Like and export csv just vm name

    Posted Jan 30, 2018 01:09 PM

    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.



  • 2.  RE: Get VMs with Tag Like and export csv just vm name
    Best Answer

    Posted Jan 30, 2018 01:16 PM

    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



  • 3.  RE: Get VMs with Tag Like and export csv just vm name

    Posted Jan 30, 2018 01:33 PM

    It's OK.

    Thank you very much



  • 4.  RE: Get VMs with Tag Like and export csv just vm name

    Posted Nov 23, 2018 05:16 AM

    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?



  • 5.  RE: Get VMs with Tag Like and export csv just vm name

    Posted Nov 23, 2018 05:40 AM

    If you know the Tag Category, you could do

    Get-TagAssignment -Category Categoryxyz



  • 6.  RE: Get VMs with Tag Like and export csv just vm name

    Posted Feb 13, 2019 04:30 PM

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


    Thanks

    LucD



  • 7.  RE: Get VMs with Tag Like and export csv just vm name

    Posted Feb 13, 2019 04:41 PM

    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



  • 8.  RE: Get VMs with Tag Like and export csv just vm name

    Posted Dec 23, 2021 02:54 AM

    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.



  • 9.  RE: Get VMs with Tag Like and export csv just vm name

    Posted Dec 23, 2021 03:01 AM

    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.



  • 10.  RE: Get VMs with Tag Like and export csv just vm name

    Posted Feb 08, 2022 03:40 PM

    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"



  • 11.  RE: Get VMs with Tag Like and export csv just vm name

    Posted Nov 23, 2018 05:50 AM

    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

    }