VMware Cloud Community
kevo12
Enthusiast
Enthusiast
Jump to solution

get datastore names from Get-ScsiLun

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

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
12 Replies
LucD
Leadership
Leadership
Jump to solution

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.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
kevo12
Enthusiast
Enthusiast
Jump to solution

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


Reply
0 Kudos
kevo12
Enthusiast
Enthusiast
Jump to solution

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

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

Reply
0 Kudos
kevo12
Enthusiast
Enthusiast
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Could it be that you have datastores with multiple extents ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
kevo12
Enthusiast
Enthusiast
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
David_A_Stewart
Contributor
Contributor
Jump to solution

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.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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}}

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
David_A_Stewart
Contributor
Contributor
Jump to solution

$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

Reply
0 Kudos
David_A_Stewart
Contributor
Contributor
Jump to solution

I think this may work

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

Reply
0 Kudos
forth_portal
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos