PowerCLI

 View Only
Expand all | Collapse all

get datastore names from Get-ScsiLun

  • 1.  get datastore names from Get-ScsiLun

    Posted Oct 07, 2011 06:19 PM

    Im  stuck here..  Can someone help me to get the datastore names for the  storage devices using the below script? My requirement is to get the naa  address (ConsoleDeviceName) and the datastore names for each storage  device.

    $esxName = "esxsrv1"

    $report = @()

    Get-ScsiLun -VmHost $esxName -LunType "disk" | %{

        $row = "" | Select Host, ConsoleDeviceName, Vendor, Model

         $row.host = $esxName

         $row.ConsoleDeviceName = $_.ConsoleDeviceName

         $row.vendor = $_.Vendor  

         $row.model = $_.Model 

         $report += $row

    }

    $report |Export-Csv  c:\Temp\results.csv -NoTypeInformation

    thanks much, Kev-o



  • 2.  RE: get datastore names from Get-ScsiLun
    Best Answer

    Posted Oct 07, 2011 06:38 PM

    Try it like this

    $esxName = "esxsrv1"

    $dsTab = @{} foreach($ds in (Get-Datastore -VMHost $esxName | where {$_.Type -eq "vmfs"})){     $ds.Extensiondata.Info.Vmfs.Extent | %{         $dsTab[$_.DiskName] = $ds.Name     } } $report = @() Get-ScsiLun -VmHost $esxName -LunType "disk" | %{     $row = "" | Select Host, ConsoleDeviceName, Vendor, Model, Datastore
        $row.host = $esxName
        $row.ConsoleDeviceName = $_.ConsoleDeviceName     $row.vendor = $_.Vendor     $row.model = $_.Model     $row.Datastore = &{         if($dsTab.ContainsKey($_.CanonicalName)){             $dsTab[$_.CanonicalName]         }     }     $report += $row} $report |Export-Csv c:\Temp\results.csv -NoTypeInformation

    The script first creates a hash table where it maps the CanonicalName with the DatastoreName.

    In the 2nd part of the script the hash table is used to look up the DatastoreName by way of the CanonicalName.



  • 3.  RE: get datastore names from Get-ScsiLun

    Posted Oct 07, 2011 08:13 PM

    Brilliant!!

    Thanks again LucD. That's exacltly what I needed.

    1 more question...   If i wanted to use something like "$esxName = get-content c:\hostlist.txt" to pull a list of servers, how can I identify the hostname in the output.

    The value for    $row.host = $esxName   is no good and Im not sure how to retrieve it properly.

    thanks

    Kev-o




  • 4.  RE: get datastore names from Get-ScsiLun

    Posted Oct 07, 2011 09:18 PM

    I fgured it out in regard to the get-content...  It's been a long day :smileyhappy:

    Added to 2 items below in red and was able to pull the host name.

    Thanks Again Luc

    $esxName = get-content c:\temp\hostlist.txt

    $dsTab = @{}

    foreach($ds in (Get-Datastore -VMHost $esxName | where {$_.Type -eq "vmfs"})){

        $ds.Extensiondata.Info.Vmfs.Extent | %{

            $dsTab[$_.DiskName] = $ds.Name

        }

    }

    $report = @()

    Get-ScsiLun -VmHost $esxName -LunType "disk" | %{

    $hostid = $_.VMHostID

    $hostsrv = get-vmhost -id $hostid

        $row = "" | Select Host, ConsoleDeviceName, Vendor, Model, Datastore

        $row.host = $hostsrv.Name

        $row.ConsoleDeviceName = $_.ConsoleDeviceName

        $row.vendor = $_.Vendor

        $row.model = $_.Model

        $row.Datastore = &{

            if($dsTab.ContainsKey($_.CanonicalName)){

                $dsTab[$_.CanonicalName]

            }

        }

        $report += $row}

    $report |Export-Csv c:\Temp\results.csv -NoTypeInformatio



  • 5.  RE: get datastore names from Get-ScsiLun

    Posted Oct 13, 2011 04:44 PM

    After running the code on multiple servers, I am finding that I have lots of host that do not report back the datastore names. After doing some digging, I see that the "diskname" value in $ds.Extensiondata.Info.Vmfs.Extent  does not match the $_.CanonicalName from Get-Scsilun (see below). Im scratching my head as to why the values do not match up and how to correct. Any ideas?

    [vSphere PowerCLI] C:\> $dstab

    Name                          Value

    ----                          -----

    vmhba1:0:0                    DMX_CL03_L00

    vmhba1:0:1                    DMX_CL03_L01

    vmhba1:0:2                    DMX_CL03_L02

    [vSphere PowerCLI] C:\> Get-ScsiLun -VmHost $esxName -LunType "disk" | select CanonicalName

    CanonicalName

    -------------

    vmhba1:1:3

    vmhba1:1:2

    vmhba1:1:1

    vmhba1:1:0

    vmhba0:0:0

    thanks,

    Kevin



  • 6.  RE: get datastore names from Get-ScsiLun

    Posted Oct 13, 2011 05:48 PM

    Could it be that you have datastores with multiple extents ?



  • 7.  RE: get datastore names from Get-ScsiLun

    Posted Oct 13, 2011 08:42 PM

    A rescan of the storage fixed the above issue..

    But I still have lots of devices in the report that do not have a datastore name that I wish to filter out. These unwated devices are mostly DR luns and/or luns that have been provisioned to the server but have not been allocated (no datastore).

    If there were only a way to run the (Get-ScsiLun -VmHost $esxName -LunType "disk") cmdlet agaist only that datastores that are seen by the host (the devices in $dstab), I'd be set...

    thanks ,

    Kev



  • 8.  RE: get datastore names from Get-ScsiLun

    Posted Oct 13, 2011 09:28 PM

    In PowerCLI 5 you could do this

    Get-VMHost | Get-Datastore | where {$_.Type -eq "VMFS"} | 
    Select Name,@{N="LUN";E={(Get-ScsiLun -Datastore $_ | Select -First 1).CanonicalName}}

    It will show you the datastorename and the LUN of the first extent.



  • 9.  RE: get datastore names from Get-ScsiLun

    Posted Jul 05, 2020 04:31 PM

    I know this is an old thread, (yet remains a good one), but has anyone figured out how to use powercli  to map inaccessible (APD) LUNs to a datastore?

    The Get-Datastore function does not return a canonical name if the scsi lun becomes disconnected, however the vCenter browser => VMHost => Storage Devices does still display the mapping for inaccessible LUNs. If you only have a few LUNs or if only 1-2 LUNs goes inaccessible, deduction is fine but when you have over a hundred mapped to a host and 20% goe inaccessible, being able to communicate that information to the storage team quickly is pretty important.



  • 10.  RE: get datastore names from Get-ScsiLun

    Posted Jul 05, 2020 04:41 PM

    Have you tried like this?

    Get-VMHost -PipelineVariable esx |

    ForEach-Object -Process {

        $esxcli = Get-EsxCli -VMHost $esx -V2

        $esxcli.storage.core.device.detached.list.Invoke()

        Select @{N='VMHost';E={$esx.Name}},

            @{N='LUN';E={$_.Device}}

    }



  • 11.  RE: get datastore names from Get-ScsiLun

    Posted Jul 05, 2020 04:55 PM

    $esxcli.storage.core.device.detached.List.Invoke(@{'device'='naa.60002ac000000000000004420001aaaf'})

    Message: EsxCLI.CLIFault.summary;

    InnerText: Detached Device List does not contain device with the name naa.60002ac000000000000004420001aaafEsxCLI.CLIFault.summary

    At line:1 char:1

    + $esxcli.storage.core.device.detached.List.Invoke(@{'device'='naa.6000 ...

    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : OperationStopped: (:) [], MethodFault

        + FullyQualifiedErrorId : VMware.VimAutomation.Sdk.Types.V1.ErrorHandling.VimException.MethodFault

    And $esxcli.storage.core.device.detached.List.Invoke() returns nothing

    This suggests the Get-DataStore cmdlet pulls in real time (as expected) while the vCenter DB (used to populate the web client) retains the canonical name to scsi lun mapping(s), I have not found a way to request directly from vCenter.

    I thought each vmhost maintains it's own datastore name/canonical name records



  • 12.  RE: get datastore names from Get-ScsiLun

    Posted Jul 05, 2020 05:16 PM

    I think this may work

    $esxcli.storage.vmfs.extent.list.Invoke() |  ?{$_.VolumeName -eq 'DatastoreName'} | Select -Expand DeviceName



  • 13.  RE: get datastore names from Get-ScsiLun

    Posted Jan 19, 2023 03:53 PM
      |   view attached

    Below is what I ran to associate the runtime name with the datastore name. I have two folder with five hosts each that I was particularly concerned about retrieving information. I grabbed the folder information for Hosts and Clusters only and grabbed the host information.

    $dsTab = @{}
    $report = @()


    foreach($esxName in ((Get-Folder -Type HostAndCluster -Name ("Folder1", "Folder 2") | Get-VMHost).Name)) {
         foreach($ds in (Get-Datastore -VMHost $esxName | where {$_.Type -eq "vmfs"})){
              $ds.Extensiondata.Info.Vmfs.Extent | %{
                $dsTab[$_.DiskName] = $ds.Name
              }
         }

         Get-ScsiLun -VmHost $esxName -LunType "disk" | %{

              $row = "" | Select Host, CanonicalName, RuntimeName, Vendor, Model, Datastore
              $row.host = $esxName
              $row.CanonicalName = $_.CanonicalName
              $row.RuntimeName = $_.RuntimeName
              $row.vendor = $_.Vendor
              $row.model = $_.Model
              $row.Datastore = &{
                   if($dsTab.ContainsKey($_.CanonicalName)){
                        $dsTab[$_.CanonicalName]
                   }
              }
              $report += $row
         }
    }

    $report |Export-Csv c:\Temp\results.csv -NoTypeInformation

    Attachment(s)

    txt
    GetDIskInfo.txt   914 B 1 version