I'm trying to identify unattached (Fibre Channel) LUNs using Get-VMHost | Get-VMHostHba | Get-ScsiLun. I thought I could do this by looking at the 'CapacityGb' property - unattached LUNs always seems to have a CapacityGb of zero. However, I've just discovered that that's not always the case.
Is there another property somewhere within the output of 'Get-ScsiLun' that definitively identifies it as being unattached? Or is there a better way to do this?
---
For some background: one of my vCenter environments has an issue where it will throw a 'duplicate datastore' error when adding a new host. As a workaround, I add the host with no datastores, attach the LUNs in the GUI (Configure -> Storage Devices), then mount the datastores from the host CLI (either via esxcfg-volume -M [datastore] or esxcli storage filesystem mount -l [datastore]). However, when there are 20 datastores, that's an annoying amount of clicking and typing, so I wrote a script to do it all for me. It usually works great, but today I encountered a host where the script attached/mounted some but not all of the datastores. I discovered that filtering on 'Get-ScsiLun | ?{$_.CapacityGb -eq 0}' is the reason why.
You could check the OperationalState of the LUN in the ScsiLun object.
When the 1st element in that array says "ok" the Lun is attached, when it says "off" the Lun is not attached.
$esxName = 'MyEsx'
$canonicalName = 'naa.6000c29f17e16a99dc4b3f8123456789'
$esx = Get-VMHost -Name $esxName
$storSys = Get-View -Id $esx.ExtensionData.ConfigManager.StorageSystem
$storSys.StorageDeviceInfo.ScsiLun |
Where-Object {$_.CanonicalName -eq $canonicalName} |
Select-Object CanonicalName, @{N='Attached';E={$_.OperationalState[0] -eq 'ok'}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You could check the OperationalState of the LUN in the ScsiLun object.
When the 1st element in that array says "ok" the Lun is attached, when it says "off" the Lun is not attached.
$esxName = 'MyEsx'
$canonicalName = 'naa.6000c29f17e16a99dc4b3f8123456789'
$esx = Get-VMHost -Name $esxName
$storSys = Get-View -Id $esx.ExtensionData.ConfigManager.StorageSystem
$storSys.StorageDeviceInfo.ScsiLun |
Where-Object {$_.CanonicalName -eq $canonicalName} |
Select-Object CanonicalName, @{N='Attached';E={$_.OperationalState[0] -eq 'ok'}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
$storSys.StorageDeviceInfo.ScsiLun
Ah, yeah, the 'OperationalState' field from that seems to work a lot better. I think that solves the problem.
Thanks!