Generating Tag list, maintaining Entity-Tag-relationship and compare against array instead of Inventory database

This post was originally published on this site

Dear all,


I am – once again – in need of some guidance with a programming issue.


In my daily routines I need to execute a script to find VMs with speficic tags and add together disk space consumed depending on the datastore they reside on and on the tags that are attached to these VMs. The script in general works fine but it runs now a little more than 2 1/2 hours and I would like to speed this up.


Currently I’m running several foreach-loops, in each loop comparing and looking for specific tags and putting together a new variable to sum up disk space consumed per Tag.



$VMsWithTags = Get-Cluster "Cluster-Win-2008" | Get-VM -Tag "Backup-Enabled" | Sort-Object


$VmsWithTags | ForEach-Object {

    if (Get-VM $_.Name | Get-TagAssignment | where {$_.Tag -like '*BCK_BLA*'}) {
        $BCK_BLA += $_.Name
        $BCK_BLA_Usage += (Get-HardDisk -VM $_.Name | Measure-Object -Sum CapacityGB).Sum


I’m doing this, as mentioned, in several loops and it takes very long as the list contains several hundred VMs.


So my idea is to simply generate ONE list as an array and write it to a file instead (if even needed!) and all the operations to compare should run against that newly generated list. I hope to achieve way better speeds because after the list in generated once all comparisons are done “locally” instead of against the vCenter Inventory database.


So in my mind generating the list would be initiated with this:

$VmsWithTags | ForEach-Object {

    Get-VM $_.Name | Get-TagAssignment



Tag                                      Entity                         ---                                      ------                         Operating System/Win2008R2_x64           Test-VM001                       System Type/Development                  Test-VM001                       Backup/Backup-Enabled                    Test-VM001                       Backup/BCK_VGA_WIN_SA                    Test-VM001                       Temporary Tag/WIN2008EOL                 Test-VM001                       Operating System/Win2008R2_x64           Test-VM002                     Backup/Backup-Enabled                    Test-VM002                     Temporary Tag/WIN2008EOL                 Test-VM002 ...   


Of course I would need to output that to a file. So far, so good.


If I could write that to a file but maintain the structure of a VM and its related Tags that would be great.


Yet again it shows I don’t know how to do this as I am not sure if I need to work with more-dimensional arrays or how to put together a construct where this Tag-Entity-relationship is kept intact so I was counting on you guys helping me overcome this and be more effective with the time it takes for the script to generate the list I can compare against.


Thank you guys in advance!




Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.