Custom report about Custom Fields in virtual infrastructure


This time i wanted to create a report on information which is put in the custom fields for my vms. You can do it very easy using the :

get-vm VM1 | get-annotation

What is more , you can specify which particular custom field you want to retrieve, for example if you cant a custom field called ContactPerson you could write:

get-vm VM1 | get-annotation -CustomAttribute "ContactPerson"

Now it is ok to use it like this, but using that : get-vm | % { get-annotation } would take you quite a while when ran in VI > 100 vms, and even more in > 1000 vms.
I have written short one liner to get all the data and put it into a csv file so i could minupulate data using filters and conditional formatting.

$(foreach ($cluster in get-cluster)  { get-view -ViewType VirtualMachine -SearchRoot $cluster.id | % { $vVM=$_; $_.Summary.CustomValue | select @{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.csv

You will receive a file in c:\customfields.csv , which will include information as on the screenshot below:

Report is generated in about 8-10 sec on a 1000vms + infrastructure, so it is faster than with the get-annotation. Feel, free to modify it, add other custom columns so it will meet your expectations.
I had to go to two places in order to complete this report, 1 is the Summary.CustomValue section from the get-view virtualmachine object, and second is the AvailableFiled section. Summary.CustomValue will tell you what values are set on which Keys(Key number), and using AvailableField you match a pair KeyNo=KeyName. I am doing a cluster loop, to get the have the information in which cluster vm resides. If needed you can change this to datacenters, or just use without cluster/datacenter container.
Update:
The above example was pulling only existing annotations data, so you will not see any vm there which did not have the custom fields filled out. I made a new script to pull data on each vm, each annotation even if the vm does not have any annotation data. What’s more, instead of having each row on “annotation entry” for vm, i have create now an output where you have 1 vm per row, and in columns you have all annotations. I hope this one will be more useful for everybody.

[array]$VMs=@()
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+=$vm
    }
}
$VMs|Export-Csv c:\annotation-report.csv

I decided to build my own object, and then fill it with all properties. It is building property columns dynamically, so when you have 5 you get 5 columns, if 10 then 10 etc…
And what is more important 😉 it is easier to read this one !

Advertisements

7 thoughts on “Custom report about Custom Fields in virtual infrastructure

  1. Hi Gus,
    In line #2,
    foreach ($cluster in get-cluster) {
    change this to
    foreach ($cluster in get-cluster -name ‘your Test cluster’) {

    • Hey Acekeeper,
      I think this should do the trick:
      [array]$VMhosts=@()
      foreach ($cluster in get-cluster -name ‘some Name’) {
      foreach ($vmhostview in (get-view -ViewType HostSystem -SearchRoot $cluster.id)) {
      $vmhost=New-Object PsObject
      Add-Member -InputObject $vmhost -MemberType NoteProperty -Name VMname -Value $vmhostview.Name
      Add-Member -InputObject $vmhost -MemberType NoteProperty -Name Cluster -Value $cluster.Name
      foreach ($CustomAttribute in $vmhostview.AvailableField){
      Add-Member -InputObject $vmhost -MemberType NoteProperty -Name $CustomAttribute.Name -Value ($vmhostview.Summary.CustomValue | ? {$_.Key -eq $CustomAttribute.Key}).value
      }
      $VMhosts+=$vmhost
      }
      }

      The way of doing is exactly the same as with VMs, it’s just now you want a viewtype of hostsystem and not virtualmachine, that’s the only change.

  2. Noob question,

    I’m trying to populate existing availablefields with a specific value. Basically a reverse of this script. I’d have a csv with the values inputted. Then I’d want to search for the matching vm and update all those values to whats in the csv. I’m having trouble producing this though.

    More specifically for this script. Can I specific a certain key value to exclude from being generated during the report?

      • Do you have an import script that would use the csv output? We are migrating VM1 from VC1 to VC2 and after migration would like to import the customer attribs.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s