Archive for September, 2010

PowerCLI vs the Perl Toolkit

September 18th 2010

As an example of Powershell vs the Perl toolkit in VMware I want to show you triggering a storage rescan on a host in both.

#!/usr/bin/perl -w

use strict;
my @hbas = `/usr/sbin/esxcfg-info \| grep vmkernel -i \| grep hba \| awk -F\. \{\'print \$29\'\}`;

foreach my $hba (@hbas) {
    system("/usr/sbin/esxcfg-rescan $hba");
    }

(From XtraVirt with comments and iSCSI stuff removed.)

It’s not awful, using grep, awk, sed etc has a long history and loads of people are good at it, but I will never be convinced it is intuitive and am far from a wizard myself.

The equivalent in Powershell is

Get-VMHost | Get-VMHostStorage -Refresh -RescanAllHba -RescanVmfs

(once you have run Connect-VIServer, note this actually does it for all hosts in the vcenter too)
Maybe you just want to do one cluster

Get-Cluster -Name CLUSTERNAME | Get-VMHost |
   Get-VMHostStorage -Refresh -RescanAllHba -RescanVmfs

The key advantage for Powershell is the richness of the object being passed around.
For example if I run

Get-VMHost | Get-ScsiLun 

What is returned is an iterator over all the LUNs on all the hosts.
What I see if I run it in the console is a representation of the attributes in a table

CanonicalN ConsoleDeviceName              LunType    CapacityMB MultipathPolicy
ame
---------- -----------------              -------    ---------- ---------------
mpx.vmh... /vmfs/devices/genscsi/mpx.v... cdrom                 RoundRobin
naa.500... /vmfs/devices/disks/naa.500... disk          2096128 RoundRobin

The great thing with powershell is I can filter, select, sort etc based on any of these and output however I want.

For example

Get-VMHost | Get-ScsiLun | Format-Table CanonicalName,CapacityMB

returns

CanonicalName                                               CapacityMB
-------------                                               ----------
mpx.vmhba3:C0:T0:L0
naa.50002ac285210609                                        2096128
naa.50002ac285070609                                        2096128

A more fun example to whet your appetite would be

Get-VMHost | Get-ScsiLun |
   where {$_.LunType -match 'disk'} |
   Select CanonicalName,CapacityMB |
   Sort-Object -Property CapacityMB |
   Export-Csv "C:\LUNsBySize.csv" -NoTypeInformation

In words

Loop through all hosts and their LUNs
Pick out the ‘disk’s
Select just CanonicalName and CapacityMB
Sort by the CapacityMB
Export it to a CSV

The file looks like

CanonicalName,CapacityMB
mpx.vmhba0:C0:T0:L0,69973
mpx.vmhba0:C0:T0:L0,69973
mpx.vmhba0:C0:T0:L0,69973
mpx.vmhba0:C0:T0:L0,139979
mpx.vmhba0:C0:T0:L0,139979
naa.50002ac285440609,2096128
naa.50002ac0a09a0609,2096128
naa.50002ac2853d0609,2096128

Without “-NoTypeInformation” it puts
#TYPE System.Management.Automation.PSCustomObject
at the top of the file.

PowerShell is great, check out this guide to learn about tricks using the pipeline.

Posted by tom under perl & powercli & powershell | 1 Comment »

Playing with PowerShell

September 15th 2010

I use powershell all the time now to manage VMware, with the PowerCLI commandlets you can achieve a lot and it matches Alan Kay’s motto

Simple things should be simple, complex things should be possible.

The simple:
I want to go and set all the LUNs in VMware to use “RoundRobin” as the MultipathPolicy

My first attempt was,

Get-VMHost | Get-ScsiLun |
    Set-ScsiLun -MultipathPolicy "RoundRobin"

Loop through the hosts, loop though each of its LUNs,
set MultipathPolicy to “RoundRobin”

Which works, but was taking ages. I was about to make separate scripts for each server when I realised I was not quite doing what I wanted with that script. I really wanted to:

Loop through the hosts, loop though each of its LUNs,
*if it was not already RoundRobin*,
set MultipathPolicy to “RoundRobin”

Get-VMHost | Get-ScsiLun |
    where {$_.MultipathPolicy -ne "RoundRobin"} |
    Set-ScsiLun -MultipathPolicy "RoundRobin"

Which ran loads faster as the “set” command still ran and took a while even if you were setting it to what it already was.

The Complex:

I took the wonderful vcheck script and made it run every night. Great email overview of the virtual infrastructure each morning.

Posted by tom under powercli & powershell & VMware | 1 Comment »