Earlier I blogged about scripting with the VI3 Perl toolkit. The toolkit makes it relatively easy to get information from VI3. In reality, I do not want to spend a lot of time writing Perl scripts, especially with the availability of PowerShell.
In PowerShell, it is easy to work with the information you retrieve. For example, if you would like to get a list of running processes with sorts and filters you do something like this:
ps | where {$_.processname -like "a*"} | sort WS -desc
The above command gets a list of processes that start with the letter a and then performs a descending sort on the working set of each process.
We will do something similar with information obtained from VirtualCenter. We will get information about disk space usage in each of the virtual machines belonging to a datacenter. Because I do not know of a way to do this directly from PowerShell, a Perl script will be used that sends the data to a PowerShell script.
In short, the steps are:
- Write a Perl script with the VI3 Perl Toolkit to connect to VirtualCenter, get all virtual machines in the datacenter and get disk space information from the virtual machines. Output this information to the screen.
- Write a PowerShell script to get the output from the Perl script and create custom objects so that you can apply filters and sorts to the dataset.
WARNING: code written by a non-programmer below :-)
The Perl script
As discussed in the previous post, you will need Perl (http://www.activestate.com) and the VI3 Perl Toolkit. Thanks to this example it was easy to write the script. It looks like this:
use strict;
use warnings;
use VMware::VIRuntime;
Vim::login(service_url => "https://server/sdk/vimService", user_name => "user", password => "password");
my $dc = Vim::find_entity_view(view_type => "Datacenter", filter => {'name' => 'dcname'} );
my $vms = Vim::find_entity_views(view_type => "VirtualMachine", begin_entity => $dc, filter => { 'guest.guestState' => 'running', 'guest.toolsStatus' => 'toolsOk' } );
foreach my $thisVM (@$vms) {
my $count=0;
while ($thisVM->guest->disk->[$count]) {
print $thisVM->name . ","
print $thisVM->guest->disk->[$count]->diskPath . ","
print $thisVM->guest->disk->[$count]->capacity / (1024*1024) . ","
print $thisVM->guest->disk->[$count]->freeSpace / (1024*1024) . "\n"
$count++;
}
}
The output of the script is something like:
server1,C:\,8000,1234
server1,D:\,4500,3500
So the format is: servername, driveletter, capacity and free space.
The PowerShell script
For this, you need the .NET Framework 2 or higher and PowerShell 1.0. The script is:
$a=perl c:\diskfree.pl
$a | foreach {
$b=$_.split(",")
$server=$b[0]
$disk=$b[1]
$capacity=[int]$b[2]
$free=[int]$b[3]
$percfree=$free / $capacity
new-object psobject |
add-member -pass NoteProperty Server $server |
add-member -pass NoteProperty Disk $disk |
add-member -pass NoteProperty Capacity $capacity |
add-member -pass NoteProperty Free $free |
add-member -pass NoteProperty PercFree $percfree }
With PowerShell, It is very easy to grab the output of a command. The first line ($a=perl c:\diskfree.pl) executes the Perl script and saves the output in $a. After that, each line in $a is split on the comma so we can get the servername, driveletter, capacity and free space.
The nice thing is that you can create your own object with new-object and attach custom properties to it. This is done to allow the sorting and grouping I talked about earlier. I also added the percentage of free space.
I saved the PowerShell script as c:\diskfree.ps1. When I run it, it outputs data is list format but with the following command:
c:\diskfree.ps1 | ft -autosize
you get something like:
Now you can do all sorts of things. For example, to get only information about the C:\ disk and to do some sorting:
C:\diskfree.ps1 | where {$_.Disk -eq "C:\"} | sort capacity -desc | ft -AutoSize
The above command results in:
Now for some fun stuff. With PowerGadgets (not free), you can even get this data in a chart easily. This command does the trick:
C:\diskfree.ps1 | where {$_.Disk -eq "C:\"} | sort capacity,free -desc | out-chart -Label Server
-Values Capacity,Free -Title "Free space"
The result is:

I hope you found this useful and that you can use this information to do some quick and dirty monitoring and reporting on VI3.



