Another post that comes from a VMTN PowerCLI Community question. Jeff wanted to find the free SCSI LUNs in his environment.
While answering that thread I was amazed there was no PowerCLI function written yet to provide this functionality. At least that was what my friend Google told me 😉
Since there exists a SDK method that makes retrieving free SCSI LUNs quite easy, the function I came up with isn’t too complex.
But it should help you in further automating the setup of your datastores.
The Script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
function Get-FreeScsiLun { <# .SYNOPSIS Find free SCSI LUNs .DESCRIPTION The function will find the free SCSI LUNs on an ESXi server .NOTES Author: Luc Dekens .PARAMETER VMHost The VMHost where to look for the free SCSI LUNs .EXAMPLE PS> Get-FreeScsiLun -VMHost $esx .EXAMPLE PS> Get-VMHost | Get-FreeScsiLun #> param ( [parameter(ValueFromPipeline = $true,Position=1)] [ValidateNotNullOrEmpty()] [VMware.VimAutomation.Client20.VMHostImpl] $VMHost ) process{ $storMgr = Get-View $VMHost.ExtensionData.ConfigManager.DatastoreSystem $storMgr.QueryAvailableDisksForVmfs($null) | %{ New-Object PSObject -Property @{ VMHost = $VMHost.Name CanonicalName = $_.CanonicalName Uuid = $_.Uuid CapacityGB = [Math]::Round($_.Capacity.Block * $_.Capacity.BlockSize / 1GB,2) } } } } |
Annotations
Line 24: The HostDatastoreSystem provides access to the method we are using
Line 26: The QueryAvailableDisksForVmfs method is called with a $null argument. That way we tell the method that we do not want to extend an existing datastore, but that we want to retrieve all free LUNs that can be used to create a datastore.
Line 27-32: The result is returned as an array of HostScsiDisk objects. With the New-Object cmdlet we pass the information that you can eventually use in the New-Datastore cmdlet.
Sample Usage
The use of this function is quite simple as the following example will show
1 2 3 |
$esx = Get-VMHost -Name MyEsx Get-FreeScsiLun -VMHost $esx | Format-List |
The result looks something like this. You can use the CanonicalName property in the Path parameter of the New-Datastore cmdlet.
You can also use the function in a pipeline construct. For example something like this
1 2 3 |
Get-Cluster -Name MyCluster | Get-VMHost | Get-FreeScsiLun | Format-List |
On my test cluster with 2 nodes this will return the following results. And as we can see the free SCSI LUNs are visible on both nodes.
Enjoy !
Jayaraghavendra
Not working for esxi 5.5
Abdel
Hi Luc,
How can I get this to list the LUN ID?
vamsi
Hi Luc,
I am looking for script which will give DS name,capacity,used,% free,UUID.
Getting the following error while loading to the powershell env.Pls help
[vSphere PowerCLI] C:tmp> . ./Get-FreeScsiLun.ps1
Missing closing ‘)’ in expression.
At C:tmpGet-FreeScsiLun.ps1:18 char:3
+ [ <<<< ValidateNotNullOrEmpty()]
Thanks
Michael
Nice work Luc, adding to toolbox.
Francois-Xavier
Nice Function, thanks LucD