VMware Cloud Community
MGod
Contributor
Contributor

Script to export VMName Notes and Tags to csv

Hello,

I'm trying to write a script that exports the VMName, Notes and Tags for all virtual machines in vCenter, and export it to csv. The script doesn't work fully as intended so far. In the section, where I retreive the Note, I'm using a filter, so the script only outputs virtual machines that do not have a specific string of text in their note. This does work as intended as long as the VM has a virtual tag associated with it. If the machine doesn't have an assigned tag, it gets disregarded and not parsed to the csv (disregarding whatever is in the note). What I'm trying to achieve is to be able to parse the VM that matches my note filter, and maybe just put a string for the tag that says "NO TAG ASSOCIATED".

Any advice would be appreciated.

Note: The while loop is used, because otherwise vCenter throws "Timed out" errors, so I just feed the script 100 VMs at a time.

0 Kudos
3 Replies
LucD
Leadership
Leadership

Would something like this do the trick?

Note that each row has a column for all Categories. The reason for that is that the Export-Csv has problems when not all rows in an array have the same number of properties.
When there is no tag for a specific category, the cell in the CSV will be empty.

PS: I'm not too sure why you would get time outs when doing a Get-VM.
Is your VCSA loaded that high?

$tagNames = Get-TagCategory | %{$_.Name}

Get-VM | where{$_.Notes -notmatch 'This is a test VM'} |ForEach-Object -Process {

    $obj = [ordered]@{

        Name = $_.Name

        Notes = $_.Notes

    }

    $tagNames | %{

        $obj.Add($_,'')

    }

    Get-TagAssignment -Entity $_ | %{

        $obj.Item($_.Tag.Category.Name) = $_.Tag.Name

    }

    New-Object PSObject -Property $obj

} | Export-Csv csv_$Date.csv -NoTypeInformation -UseCulture


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

0 Kudos
MGod
Contributor
Contributor

Hi LucD,

Yes indeed this accomplishes exactly what I want, thank you very much. As for your question - Yes the VCSA has a few thousand VMs on it.

0 Kudos
LucD
Leadership
Leadership

Perhaps it is easier to change the WebOperationTimeoutSeconds temporarily with the Set-PowerCLIConfiguration cmdlet?
Keeps the code simpler :smileycool:


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

0 Kudos