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.