Well, actually, I should add “and a bit of Perl” to the title. But first, let me explain the idea behind the script. It is a simple script for administrators of VMware Infrastructure to do some reporting on the VMFS datastores in a datacenter. It does some things like calculating the total percentage of free space but that is not the point. The point is: “how do we get to the datastore information and what can we do with it in PowerShell”.
To work with VMware Infrastructure from scripts and custom programs, VMware provides a web service running on either VirtualCenter (a management server) or individual ESX boxes. Theoretically, you can connect to the web service from PowerShell using all available techniques that PowerShell provides. The problem is that the VMware web service is so complex that I do not want to spend much time with it (hey, I am not a developer). Luckily, there is the VI Perl Toolkit and instructions on how to install and use it on Windows. The PowerShell script uses the output of a Perl script that use the VI Perl Toolkit to get the datastore information. The Perl script (getds.pl) looks like this:
use strict;
use warnings;
use VMware::VIRuntime;Vim::login(service_url => “https://$ARGV[0]/sdk/vimService”, user_name => “$ARGV[1]”, password => “$ARGV[2]”);
my $dc = Vim::find_entity_view(view_type => “Datacenter”, filter => {’name’ => $ARGV[3]} );
my $ds = $dc->datastore;print “Name,Type,Capacity,FreeSpace\n”
foreach(@$ds) {
my $ds_ref=Vim::get_view(mo_ref => $_);
if($ds_ref->summary->multipleHostAccess eq “true”) {
print “”" . $ds_ref->info->name . “”,”
print $ds_ref->summary->type . “,”
print $ds_ref->summary->capacity / (1024*1024) . “,”
print $ds_ref->info->freeSpace / (1024*1024) . “\n”
}}
The Perl script needs to be run like this:
perl getds.pl <vcsserver> <username> <password> <datacenter_name>
The output of the script can be saved as csv for further processing. The output would look like this:
Name,Type,Capacity,FreeSpace
“DS1″,VMFS,255744,165082
“DS2″,VMFS,255744,236510
“DS3″,VMFS,255744,211395
What I would like to do is get the output from the Perl script and turn it into something usable in PowerShell. That is not too difficult with the following PowerShell script (get-ds.ps1 without almost any error handling, just enough for a lazy admin):
# check if there are 2 parameters
if ($args.Count -ne 2) {
Write-Error “You need to specify two parameters: vcserver and datacenter”
Exit
}$path_to_perl_script=”c:\getds.pl”
$path_to_csv=”c:\datastores.csv”
$vc_server=$args[0]
$datacenter=$args[1]# use get-credential to get the user account (domain\user) and password (secure string)
$cred=(get-credential)# this function is used to convert the securestring into cleartext for passing it to the perl script
function Convert-SecureString($sstring) {
$BSTR = [System.Runtime.InteropServices.marshal]::SecureStringToBSTR($sstring)
$ClearString=[System.Runtime.InteropServices.marshal]::PtrToStringAuto($BSTR)
[System.Runtime.InteropServices.marshal]::ZeroFreeBSTR($BSTR)
$ClearString
}# run the perl script and export the text results to a csv file ($path_to_csv gets overwritten)
perl $path_to_perl_script $vc_server $cred.username (convert-securestring($cred.password)) $datacenter > $path_to_csv# import from the csv file into a PSCustomObject, simpler than creating your own custom object
$dsps=import-csv $path_to_csv# calculate the total capacity and total free space with measure-object
$total_capacity=($dsps | measure-object -Property capacity -sum).sum / 1024
$total_free=($dsps | measure-object -Property freespace -sum).sum / 1024# write out some information and format
write-host “`nCapacity Info`n=============” -foregroundcolor yellow
write-host (”Total capacity: {0:N0} GB” -f $total_capacity)
write-host (”Total free: {0:N0} GB” -f $total_free)
write-host (”Percentage free: {0:P0}`n” -f ($total_free / $total_capacity))# return $dsps so the admin can do some further processing with it
return $dsps
The get-ds.ps1 script takes two parameters: the name of the VirtualCenter server and the name of a datacenter object in VirtualCenter. For example:
./get-ds.ps1 vcserver01 Brussels
The output would be (datastore names removed):
The admin can also call the PowerShell script as follows:
$ds=./get-ds.ps1 vcserver01 Brussels
In that case, he would see the calculations but not the table with an overview of datastores. The data for that table is stored in the $ds variable (PSCustomObject) and can be used to perform further analysis. For instance:
$ds | ? { ([int]$_.FreeSpace / [int]$_.Capacity) -lt 0.4}
The above would only return the datastores with less than 40% free space.
The next step is to bypass the Perl script completely and do it all in PowerShell but then I need to explore the VMware SDK in more detail.

One Comment
Just an FYI, your scripts do not work when pasted. They are invalid characters such as the ” and ‘ characters. When I fixed the invalid characters there are still errors in your script. I would really love to have something like this as you script seems to be the only one I can find but alas it does not work.
One Trackback/Pingback
[…] Getting VMFS datastore information with PowerShell […]
Post a Comment