VMware Cloud Community
catman
Contributor
Contributor

PowerCLI - Datastores and LUNs.... Argh!!!

Hello

I am new to vSphere PowerCLI (just over a month of dabbling), and I'm building up a script that we can use to perform various operations.

My current hurdle is how to find out what LUN a datastore is on. The vSphere Client shows this data with apparent ease.

I can obviously get info for a datastore:

$objDataStore = get-datastore -name "PLOEVA01_VMFS_001"

$objDataStore | Format-List

FreeSpaceMB : 467655

CapacityMB : 511744

Accessible : True

Type : VMFS

Id : Datastore-datastore-113

Name : PLOEVA01_VMFS_001

...and I can then get a .NET view, and delve a bit deeper:

$objDataStoreView = Get-View -id $objDataStore.id

$objDataStoreView

Info : VMware.Vim.VmfsDatastoreInfo

Summary : VMware.Vim.DatastoreSummary

Host : {VMware.Vim.ManagedObjectReference, VMware.Vim.ManagedObjectReference}

Vm : {VMware.Vim.ManagedObjectReference, VMware.Vim.ManagedObjectReference, VMware.Vim.ManagedObjectRe

ference, VMware.Vim.ManagedObjectReference}

Browser : VMware.Vim.ManagedObjectReference

Capability : VMware.Vim.DatastoreCapability

Parent : VMware.Vim.ManagedObjectReference

CustomValue : {}

OverallStatus : green

ConfigStatus : gray

ConfigIssue : {}

EffectiveRole : {-1, 5}

Permission : {}

Name : PLOEVA01_VMFS_001

DisabledMethod : {}

RecentTask : {}

DeclaredAlarmState : {alarm-9.datastore-113}

TriggeredAlarmState : {}

AlarmActionsEnabled : True

Tag : {}

Value : {}

AvailableField : {}

MoRef : VMware.Vim.ManagedObjectReference

Client : VMware.Vim.VimClient

...the Info and Summary properties give some additional info:

$objDataStoreView.info

Vmfs : VMware.Vim.HostVmfsVolume

Name : PLOEVA01_VMFS_001

Url : sanfs://vmfs_uuid:4a6daf9a-fde9d1f2-ee8b-001b784495e2/

FreeSpace : 490371809280

MaxFileSize : 274877906944

Timestamp : 03/11/2009 14:18:46

DynamicType :

DynamicProperty :

$objDataStoreView.summary

Datastore : VMware.Vim.ManagedObjectReference

Name : PLOEVA01_VMFS_001

Url : sanfs://vmfs_uuid:4a6daf9a-fde9d1f2-ee8b-001b784495e2/

Capacity : 536602476544

FreeSpace : 490371809280

Uncommitted : 8694775296

Accessible : True

MultipleHostAccess : True

Type : VMFS

DynamicType :

DynamicProperty :

...but I just cannot find a correlation between this and a LUN. In desperation, I knocked up a routine (below) which enumerates the fibre HBAs in a given host, enumerates the visible LUNs, then the paths, picks the active paths and reports any paths that lead to a fibre disk. The last step would be to list the datastores on this disk. But how?

Any thoughts... anyone?

Thank you

Simon Catlin

-


MY FUNCTION FOR ENUMERATING A HOST'S STORAGE:

<See EXAMPLE_CODE.TXT attachment>

EXAMPLE OUTPUT:

<See EXAMPLE_OUTPUT.txt attachment>

Reply
0 Kudos
12 Replies
catman
Contributor
Contributor

Can't seem to stop this editor mangling my code example. If you'd like a copy of the code to see what I'm trying to do, let me know.

Thanks

Reply
0 Kudos
monahancj
Contributor
Contributor

Not sure if this is what you're asking for, but here's my guess.

$t = get-vmhost -datastore datastorename | Select-Object -Last 1 -Property Name
get-scsilun -vmhost $t.name | format-table -autosize

CanonicalName ConsoleDeviceName LunType CapacityMB MultipathP

olicy

-


-


-


-


-


vmhba1:0:13 /vmfs/devices/disks/vml.02000d00006006016041561a00b82d742836f0dc11524149442035 disk 1099247 MostRec...

vmhba1:0:4 /vmfs/devices/disks/vml.02000400006006016041561a00fc1f387733f0dc11524149442035 disk 1099247 MostRec...

vmhba1:0:8 /vmfs/devices/disks/vml.02000800006006016041561a007c1b9e9533f0dc11524149442035 disk 1099247 MostRec...

vmhba1:0:11 /vmfs/devices/disks/vml.02000b00006006016041561a00f2da0ef537f0dc11524149442035 disk 824435 MostRec...

Reply
0 Kudos
catman
Contributor
Contributor

Thanks for that.

It's almost what I need.

This gives me hosts attached to a datastore, and then the SCSI LUNs for

each host. The missing bit for me is tying the LUNs with the original

datastore.

Essentially, I'm trying to understand which datastores are on a LUN, so

that we can perform SAN operations (snapshots, mirroring, etc), and

understand which datastores we are affecting.

I appreciate that a LUN can contain more than one datastore, and a

datastore can have extents on multiple LUNs.

I'm now exploring the depths of paths and extents...

Regards

Simon

Reply
0 Kudos
catman
Contributor
Contributor

I now have this working. Essentially I needed to examine the paths for each HBA, and then look for datastore extents.

I'll now close this one off.

Reply
0 Kudos
monahancj
Contributor
Contributor

Catman, would you post the solution please? Thanks.

Reply
0 Kudos
catman
Contributor
Contributor

Sure. Code and example output attached.

Cheers

Reply
0 Kudos
malabelle
Enthusiast
Enthusiast

Bizarre,

when I try your script, it does nothing.

is there a switch I'm missing?

.\script.ps1 /HOST:server.domain.local

thanks

vExpert '16, VCAP-DCA, VCAP-DCD
Reply
0 Kudos
LucD
Leadership
Leadership

Try calling the script like this

.\script.ps1 server.domain.local

or

.\script.ps1 -VIHost server.domain.local


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

Reply
0 Kudos
OsburnM
Hot Shot
Hot Shot

Hey Guys-- I'm looking for something limilar but cant get it down.

I'd like to get a list of datastore names, their size, & the CanonicalName of ALL datastores in a given Datacenter and output that to a csv.

Is this possible?

Thanks!

Reply
0 Kudos
jade_
Contributor
Contributor

@ OsburnM:

This may be close to what you are looking for   (borrowed heavily from Luc's examples 🙂  ) :

If ((Get-PSSnapin "VMware.VimAutomation.Core" -ErrorAction SilentlyContinue) -eq $null) {
Add-PSSnapin "VMware.VimAutomation.Core"
}
# Define email recipient array
$rcpt = @()
$rcpt += "user@domain.com"
$subject = "Virtual Environment San Device Report"
Connect-VIServer <Your VC Server>
$report = @()
$datacenter = "Your DC Name"
#$datacenter = Get-Datacenter
foreach ($cluster in ($datacenter |Get-Cluster)) {
$vmhost = $cluster |Get-VMHost | sort name | select -First 1
$dsTab = @{}
foreach ($ds in (Get-Datastore -VMHost $vmhost | where {($_.Type -eq "vmfs") `
-and ($_.name -notlike "*local*") `
-and ($_.CapacityMB -ge "100000")})){
    $ds.Extensiondata.Info.Vmfs.Extent  | %{
$dsTab[$_.DiskName] = $ds.Name
    }
}
Get-ScsiLun -VmHost $vmhost -LunType "disk" | `
where {($_.CapacityMB -ge "100000")} `
| %{
    $row = "" | Select Host, ConsoleDeviceName, RunTimeName, CapacityMB, Vendor, Model, Datastore
    $row.host = $vmhost.name
    $row.ConsoleDeviceName = $_.ConsoleDeviceName
$row.CapacityMB = $_.CapacityMB
$row.RunTimeName = $_.RunTimeName
    $row.vendor = $_.Vendor
    $row.model = $_.Model
    $row.Datastore = &{
        if ($dsTab.ContainsKey($_.CanonicalName)){
            $dsTab[$_.CanonicalName]
        }
    }
$report += $row
}
}
Disconnect-VIServer * -Confirm:$false
$report | Sort-Object -Property Frame |Export-Csv c:\Reports\LUN_Scan_results.csv -NoTypeInformation
foreach ($person in $rcpt){
Send-MailMessage -to $person -from USER@DOMAIN.com -sub $subject -smtpserver YOUR_SMTP_SERVER@Domain.com `
-attachments "c:\Reports\LUN_Scan_results.csv"
}
Reply
0 Kudos
tsentekin
Contributor
Contributor

this is great for sorting out but;

can i get only the lun which has "vmhba1:0:4"  for example?  i want to find a particular lun with the runtimename's lun ID section, which is the lun ID given by the storage mapping.

Cheers,

Reply
0 Kudos
jade_
Contributor
Contributor

Luc has a great function here, and does a much better job of explaining the nuances:

http://www.lucd.info/2010/10/17/runtime-name-via-extensiondata-and-new-viproperty/

HTH!

j

Reply
0 Kudos