Find free SCSI LUNs

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

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

The result looks something like this. You can use the CanonicalName property in the Path parameter of the New-Datastore cmdlet.

freelun1

You can also use the function in a pipeline construct. For example something like this

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.

freelun2

Enjoy !

5 Comments

    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

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.