Chicken Counter Script

What we have today, was brought up on #ESX on EfNet. They were looking for a script to generate output similar to what you get by clicking on “Hosts and Clusters”, selecting “File > Export > Export List…”

Like this:

2009-03-12_1556

2009-03-12_1558

What I came up with is not quite as pretty, but, it works, and you can schedule it using the Windows Task Scheduler. First the code:

   1:  Begin {
   2:      $hostCount= @{
   3:          Name = "hostCount"
   4:          Expression = { $_ | get-vmhost | measure-object | %{$_.Count} }
   5:      }
   6:      $vmCount = @{
   7:          Name = "vmCount"
   8:          Expression = { $_ | get-vm | measure-object | %{$_.Count}  }
   9:      }
  10:  }
  11:   
  12:  Process {
  13:      $InputTypeName = $_.GetType().Name
  14:      if ( $InputTypeName -eq "DatacenterImpl" ) {
  15:          $output = $_ | select Name, $hostCount, $vmCount
  16:          return $output
  17:      }
  18:  }

I’ve saved this in a file creatively called export-list.ps1. It requires you to be connected to a vCenter server, and accepts a datacenter object ($InputTypeName –eq “DatacenterImpl”) from the pipeline. From there it takes each, and spits them out, like this:

[VI Toolkit] C:\> get-datacenter | .\scripts\export-list.ps1

Name                                        hostCount                   vmCount

—-                                        ———                   ——-

dev                                              1                         3

stage                                            1                         5

prod                                             1                         8

Sexy, right? Not so much, but this is where PowerShell and the VI Toolkit shine, in their flexibility. So you want HTML? Check this out:

[VI Toolkit] C:\> get-datacenter | .\scripts\export-list.ps1 | ConvertTo-HTML > report.html

Just open report.html, and run with it. Scheduling this in Task manager isn’t that much harder, but will remain a task for the reader.

One thought on “Chicken Counter Script”

Comments are closed.