2 Replies Latest reply on May 23, 2012 2:54 AM by LucD

    Get all custom fields for vms question

    Grzesiekk Expert


        i am trying to get a list of all custom fields for vms. I think i am doing something wrong here, can somebody take a look and tell me if there is easier way to get this data ?

      get-view -ViewType VirtualMachine | % { $vVM=$_; $_.Summary.CustomValue | select Value,Key,@{N="Key Name";E={$vKey=$_.Key;($vVM.AvailableField|?{$_.Key -eq $vKey}).Name}} }


      This works fine, pretty fast too. But what worries me here is that i have to get the vm view  with tis customValue, and then pair its value with its key name, so i had to go to AvailableField, check the key number, and  get its name to build this table.

      I am wondering why i can't see the value  when you list the (get-vm name |get-view).AvailableField , you can see the descriptions of the custom fields, but there is no value. I don't know any other way, so i went to summary.customvalue, as this displays the vale and the key, but it does not display the key name, so then i paired the key with the key name. I think the way i did this is not efficient.

      Do someone know if it is possible to get this data in easier approach ?


      Thank you in advance


        • 1. Re: Get all custom fields for vms question
          LucD Guru
          User ModeratorsCommunity WarriorsvExpert

          I'm afraid that is the way to do it when using VirtualMachine objects.

          The alternative is


          Get-VM | Get-Annotation 


          But that is probably way slower in a bigger environment

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: Get all custom fields for vms question
            Grzesiekk Expert

            Hey LucD,

              thanks for answer, yeah i already tried the get-annotation, it works as expected, vey easy to use and so on, its just that i got large VI here, so i need to write it using get-view.

            Ok then if this is the only way then its fine with me, i just wanted to double check this.



            PS. if anyone would like to use this approach here is the command i have built:


            $(foreach ($cluster in get-cluster)  { get-view -ViewType VirtualMachine -SearchRoot $cluster.id | % { $vVM=$_; $_.Summary.CustomValue | s
            elect @{N="VM Name";E={$vVM.Name}},@{N="Cluster";E={$cluster.name}},Value,Key,@{N="Key Name";E={$vKey=$_.Key;($vVM.AvailableField|?{$_.Key -eq $vKey}).Name}} }
            }) | export-csv c:\customfields-report.csv


            You will receive a csv table with vmname,cluster name in which vm is, the value of custom field,the key nr of custom field, the custom field name.





            i made small modifications so here you go:


            foreach ($cluster in get-cluster)  {
                foreach ($vmview in (get-view -ViewType VirtualMachine -SearchRoot $cluster.id)) {
                    $vm=New-Object PsObject
                    Add-Member -InputObject $vm -MemberType NoteProperty -Name VMname -Value $vmview.Name
                    Add-Member -InputObject $vm -MemberType NoteProperty -Name Cluster -Value $cluster.Name
                    foreach ($CustomAttribute in $vmview.AvailableField){
                        Add-Member -InputObject $vm -MemberType NoteProperty -Name $CustomAttribute.Name -Value ($vmview.Summary.CustomValue | ? {$_.Key -eq $CustomAttribute.Key}).value
            $VMs|Export-Csv c:\annotation-report.csv


            37 sec execution time on 1500 vms infrastructure

            This one gets information on ALL vms, the one before was getting information only in case they were existing. So it was ingoring vms which did not have annotations. In the new on, you will gett csv table for all annatations, for all vms, if vm does not have any, it wil lhave blank field in annatation sections.