PowerCLI script to get NAA and Datastore name of all storage on ESX Server

Use the following script to get a list of Datastores, their respective capacities and their corresponding NAAs on an ESX Server:

$esxName = "esx01.vi.local"

Connect-VIServer vs01.vi.local

New-VIProperty -Name lunDatastoreName -ObjectType ScsiLun -Value {
    param($lun)

    $ds = $lun.VMHost.ExtensionData.Datastore | %{Get-View $_} | `
        where {$_.Summary.Type -eq "VMFS" -and
            ($_.Info.Vmfs.Extent | where {$_.DiskName -eq $lun.CanonicalName})}
    if($ds){
        $ds.Name
    }
} -Force | Out-Null

Get-VMHost -Name $esxName | Get-ScsiLun | Select CanonicalName, CapacityMB, lunDatastoreName

To export the results to a CSV file run same script adding the following to the end of the last line:

 | Export-csv C:\DatastoreInfo.csv

I can’t remember where I got this script from, whoever it was, I thank-you sincerely! The Get-ScsiLun command line reference can be found here.

Credit to Whowe who added his method in the comments below which worked perfectly. His script code is directly below, I have added some connection parameters to show you how I would use it to get datastore info:

$esxName = "esx01.vi.local"

Connect-VIServer vs01.vi.local

Get-VMHost -Name $esxName | Get-Datastore |

Where-Object {$_.ExtensionData.Info.GetType().Name -eq "VmfsDatastoreInfo"} |

ForEach-Object {

if ($_)

{

$Datastore = $_

$Datastore.ExtensionData.Info.Vmfs.Extent |

Select-Object -Property @{Name="Name";Expression={$Datastore.Name}},

DiskName

}

}

The output from the script is below which shows NAA as well as Datastore info (but not capacity):

[vSphere PowerCLI] C:\Admin> .\GetDSInfo.ps1

Name                           Port  User
----                           ----  ----
vs01.vi.local                  443   DOMAIN\admin

Name     : datastore-1-R1
DiskName : naa.6848f690ec8be200191117982eb0b641

Name     : datastore-2-R5
DiskName : naa.60000970000295800173533030384646

Hope this benefits someone out there and thanks to Whowe for his contribution!

  • Whowe

    Where are you getting the property “lunDatastoreName”? I don’t see it as an available property.

    Id

    CanonicalName

    RuntimeName

    Key

    LunType

    Model

    SerialNumber

    Vendor

    ConsoleDeviceName

    CapacityMB

    CapacityGB

    MultipathPolicy

    CommandsToSwitchPath

    BlocksToSwitchPath

    HostId

    VMHostId

    VMHost

    Uid

    IsLocal

    ExtensionData

    • japinator

      Yes this property does’t normally exist, it was created using New-VIProperty which queries VMFS Extent Info from VMHost.ExtensionData.Datastore. I will review my work notes to see exactly what is run but there should certainly be more code that what is currently being displayed. Thanks for pointing this out. I will get it corrected when I get the chance.

      • Whowe

        I was able to get the info I needed with:

        Get-Datastore |

        Where-Object {$_.ExtensionData.Info.GetType().Name -eq “VmfsDatastoreInfo”} |

        ForEach-Object {

        if ($_)

        {

        $Datastore = $_

        $Datastore.ExtensionData.Info.Vmfs.Extent |

        Select-Object -Property @{Name=”Name”;Expression={$Datastore.Name}},

        DiskName

        }

        }

        • japinator

          Nice. Will give that a try when the need arises. Thanks for sharing buddy!

        • japinator

          I amended the post after finally getting round to it. I also added your code. Thanks again for sharing!

  • Tom Courtney

    Get-Datastore |
    Where-Object {$_.ExtensionData.Info.GetType().Name -eq “VmfsDatastoreInfo”} |
    ForEach-Object {
    if ($_)
    {
    $Datastore = $_
    $Datastore.ExtensionData.Info.Vmfs.Extent |
    Select-Object -Property @{Name=”Name”;Expression={$Datastore.Name}},
    DiskName
    }
    }

    from: http://rvdnieuwendijk.com/2012/06/03/use-powercli-to-find-the-datastore-from-a-disk-name/

    • http://www.vsysad.com japinator

      Thanks for sharing!