PowerCLI vs the Perl Toolkit

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. [perl]

!/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”); }/perl

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 [powershell]Get-VMHost | Get-VMHostStorage -Refresh -RescanAllHba -RescanVmfs/powershell Maybe you just want to do one cluster [powershell]Get-Cluster -Name CLUSTERNAME | Get-VMHost | Get-VMHostStorage -Refresh -RescanAllHba -RescanVmfs[/powershell] The key advantage for Powershell is the richness of the object being passed around. For example if I run [powershell]Get-VMHost | Get-ScsiLun [/powershell] 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 [code] 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 [/code] The great thing with powershell is I can filter, select, sort etc based on any of these and output however I want.

For example [powershell]Get-VMHost | Get-ScsiLun | Format-Table CanonicalName,CapacityMB[/powershell] returns [code] CanonicalName CapacityMB


mpx.vmhba3:C0:T0:L0 naa.50002ac285210609 2096128 naa.50002ac285070609 2096128[/code]

A more fun example to whet your appetite would be [powershell] Get-VMHost | Get-ScsiLun | where {$_.LunType -match ‘disk’} | Select CanonicalName,CapacityMB | Sort-Object -Property CapacityMB | Export-Csv “C:\LUNsBySize.csv” -NoTypeInformation [/powershell] 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 [code] 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 [/code] 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.