VMware Cloud Community
KevinRSmith
Contributor
Contributor
Jump to solution

Load Balance LUN Paths, Where-Object error

Greetings,

I have started modifying the script from:

http://vmjunkie.wordpress.com/2009/01/29/balancing-lun-paths-on-your-esx-hosts-with-powershell/

to fit our enviroment. We have our utility LUNs labeled similar to out VM LUNs, but they do not have the same number of paths. Here is the where-object select statement I am using:

Script SNIP:

$luns = $VMHost|get-scsilun -luntype disk|

where-object {$_.ConsoleDeviceName -like "/vmfs/devices/disks/vml*"} |

where-object {$_.CanonicalName -like "vmhba0:0:*"}|Sort-Object CanonicalName

$firstLUNPaths = Get-ScsiLunPath $lun[0]

$numPaths = $firstLUNPaths.Length

The script work fine on the first host in the cluster, but I recieve this error on each host afterwards:

Err:

Unable to index into an object of type VMware.VimAutomation.Client20.Host.ScsiLunImpl.

At C:\hp\Tools\VM_Tools\Scripts\Pathing\Set_Cluster_SAN_Path_t.ps1:37 char:40

+ $firstLUNPaths = Get-ScsiLunPath $lun[0 <<<< ]

Line 37 is:

$firstLUNPaths = Get-ScsiLunPath $lun[0]

If I remove the portion of the select statement:

where-object {$_.CanonicalName -like "vmhba0:0:*"}

The error goes away. Since this is the portion of the select statement that gets the VM LUNs I really would like this to work.

Thanx in advance!

Kevin

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Are you sure that $lun is an array when you get the error ?

Could be that there is only 1 ScsiLunImpl object. Hence the index error.

You can try asking the type of the $lun variable

$luns = $VMHost|get-scsilun -luntype disk|
where-object {$_.ConsoleDeviceName -like "/vmfs/devices/disks/vml*"} |
where-object {$_.CanonicalName -like "vmhba0:0:*"}|Sort-Object CanonicalName

($lun.gettype()).Name

$firstLUNPaths = Get-ScsiLunPath $lun[0]
$numPaths = $firstLUNPaths.Length

This will say "object[]" when it's an array but "ScsiLunImpl" when it's a single object.

An easy way to solve this would be to make it an array even when there is only 1 object

$luns = @($VMHost|get-scsilun -luntype disk|
where-object {$_.ConsoleDeviceName -like "/vmfs/devices/disks/vml*"} |
where-object {$_.CanonicalName -like "vmhba0:0:*"}|Sort-Object CanonicalName)
$firstLUNPaths = Get-ScsiLunPath $lun[0]
$numPaths = $firstLUNPaths.Length


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Are you sure that $lun is an array when you get the error ?

Could be that there is only 1 ScsiLunImpl object. Hence the index error.

You can try asking the type of the $lun variable

$luns = $VMHost|get-scsilun -luntype disk|
where-object {$_.ConsoleDeviceName -like "/vmfs/devices/disks/vml*"} |
where-object {$_.CanonicalName -like "vmhba0:0:*"}|Sort-Object CanonicalName

($lun.gettype()).Name

$firstLUNPaths = Get-ScsiLunPath $lun[0]
$numPaths = $firstLUNPaths.Length

This will say "object[]" when it's an array but "ScsiLunImpl" when it's a single object.

An easy way to solve this would be to make it an array even when there is only 1 object

$luns = @($VMHost|get-scsilun -luntype disk|
where-object {$_.ConsoleDeviceName -like "/vmfs/devices/disks/vml*"} |
where-object {$_.CanonicalName -like "vmhba0:0:*"}|Sort-Object CanonicalName)
$firstLUNPaths = Get-ScsiLunPath $lun[0]
$numPaths = $firstLUNPaths.Length


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
KevinRSmith
Contributor
Contributor
Jump to solution

LucD, I attempted the change you suggested and recieved the same error.

I am new to powershell scripting, I am not sure what you are refering to when you say "You can try asking the type of the $lun variable"

Thanx!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Just noticed: in line 1 you assign the value to a variable called $luns.

And in line 4 you reference a variable called $lun.

Is that a typo in your script SNIP or is that perhaps the cause of the problem.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

KevinRSmith
Contributor
Contributor
Jump to solution

Good catch... Yes that was an actual typo in the script. I went back to the original script before we found our issue witht he utility LUNs.

I had modified the variable during testing processes.

Now using the array suggestion works great!!

Thank you for your time!!

Reply
0 Kudos