VMware Cloud Community
padulka
Hot Shot
Hot Shot
Jump to solution

Export list of Hosts and tags

I need to export all hosts (and only one VM of every host) contained into a specific folder with the custom tags to a csv. The csv will contain as follow:

vcenter,host,vm,tagname

the naming convation are the following:

host: hostxxxx

vm: vmxxxx

where "xxxx" value it's unique for each items (ex: host1234 contain vm1234 - host5678 contain vm5678)

I try a lot of command without success.

Anyone could help me?

Regards

Labels (1)
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You mean something like this?

 

Get-VMHost -PipelineVariable esx |
ForEach-Object -Process {
    $vm = Get-VM -Name ("vm$($esx.Name.TrimStart('host'))") -Location $esx
    Get-TagAssignment -Entity $vm -PipelineVariable tag |
    ForEach-Object -Process {
        New-Object -TypeName PSObject -Property @{
            VMHost = $esx.Name
            VM = $vm.Name
            Tag = "$($tag.Tag.Category.Name)/$($tag.Tag.Name)"
        }
    }
} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture

 


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

View solution in original post

Reply
0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

You mean something like this?

 

Get-VMHost -PipelineVariable esx |
ForEach-Object -Process {
    $vm = Get-VM -Name ("vm$($esx.Name.TrimStart('host'))") -Location $esx
    Get-TagAssignment -Entity $vm -PipelineVariable tag |
    ForEach-Object -Process {
        New-Object -TypeName PSObject -Property @{
            VMHost = $esx.Name
            VM = $vm.Name
            Tag = "$($tag.Tag.Category.Name)/$($tag.Tag.Name)"
        }
    }
} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture

 


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

Reply
0 Kudos
padulka
Hot Shot
Hot Shot
Jump to solution

Hi @LucD,

I failed to disclose that the tag I need to export is on the host. In my lab names are:

host: q8esx65-03.lab.local - VM: ITWKSPPE03

host: q8esx65-01.lab.local - VM: ITWKSPPE01

with your script add to the VM name ".lab.local" and return me error.
 
Thanks
Reply
0 Kudos
padulka
Hot Shot
Hot Shot
Jump to solution

Hi @LucD ,

I solved error changing your command as follow:

Get-VMHost -PipelineVariable esx |
ForEach-Object -Process {

$hostlab = [string]$esx.Name.TrimStart('q8esx65-')

$hostlab = $hostlab -replace "..........$"
$vm = Get-VM -Name ("ITWKSPPE$($hostlab)") -Location $esx
Get-TagAssignment -Entity $esx -PipelineVariable tag |
ForEach-Object -Process {
New-Object -TypeName PSObject -Property @{
VMHost = $esx.Name
VM = $vm.Name
Tag = "$($tag.Category.Name)/$($tag.Name)"
}
}
}

But the output it's:

VM Tag VMHost
-- --- ------
ITWKSPPE03 /com.vmware.cis.tagging.TagAssociationModel q8esx65-03.lab.local
ITWKSPPE01 /com.vmware.cis.tagging.TagAssociationModel q8esx65-01.lab.local

with a wrong tag.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I had an error in the Tag property, I corrected that in the code above.

Your latest info makes no sense to me, I'm not sure what you are saying there.


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

Reply
0 Kudos
padulka
Hot Shot
Hot Shot
Jump to solution

LucD after you correct the script works fine.

The customization regarding my specific lab configuration.

I've only change the tag source from $vm to $esx 

Thanks a lot.

 

Reply
0 Kudos