VMware Cloud Community
harkamal
Expert
Expert

Get-View -- how to get multiple or sub-views of viobject

I need the storage view, so I fire this query:

     $hostCol = Get-View -viewType HostSystem -Property Config.Product,Runtime,ConfigManager.StorageSystem  (takes 3 seconds for 70 hosts)

It does not give me the view, but a MoRef, I have to ask the server again for the view.

     $ds = get-View $hostCol[0].ConfigManager.StorageSystem

Can this be achieved in 1 shot ? performance optimized ? There are 70-80 hosts and each 2nd view for StorageSystem takes about 1-2 seconds per host

Thanks

0 Kudos
4 Replies
LucD
Leadership
Leadership

The StorageSystem property contains a MoRef so it's normal you have to do the Get-View to get at the actual HostStorageSystem object.

I can't think of a way to do this in one go since there is no API that offers this functionality.


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

0 Kudos
admin
Immortal
Immortal

Actually there is a way to do this with a single server call - using the PropertyCollector. The downside is that you'll end up writing a lot more code which will be fairly difficult to read.

Could you describe your use-case - what do you need the host's StorageSystems for? What makes it performance critical?

Regards,

Dimitar

0 Kudos
harkamal
Expert
Expert

That's just an example requirement, i have scripts that run everyday for some checks, takes about half an hour on a vc. there are 30+ vc.

Recently, I have been playing with selective retrieval of views and properties, that makes my scripts incredibly fast, 50x faster.

So Now I need to know how to retrieve multiple view in a single trip to vc server, to expedite and cache objects in one go and process them in memory.

Can you give me code to fetch 2-3 views in one go ? pls pls pls Smiley Happy

0 Kudos
admin
Immortal
Immortal

Here's an example showing how to retrieve the views of the StorageSystems from all hosts on the VC. Basically you define a starting object from which to start traversing the object hierarchy on the server (in this case - the root folder of the VC). You define the path that you want to follow when traversing (this is done through the MoRef links between the objects) and you define the properties which you are interested in (in this case - "multipathStateInfo" and "storageDeviceInfo" properties of HostStorageSystem object). The result you get contains the values of these properties. If you need you can construct the view objects (as seen in the last part of the script). I've also attached the script as a file since the community doesn't preserve the formatting:

$sc = Get-View -Id ServiceInstance
$pc = Get-View -Id PropertyCollector-propertyCollector
# Selection specs are needed for cyclic traversal of objects
$sspecFolder = New-Object VMware.Vim.SelectionSpec
$sspecFolder.Name = "tspecFolder"
$sspecDatacenter = New-Object VMware.Vim.SelectionSpec
$sspecDatacenter.Name = "tspecDatacenter"
$sspecCluster = New-Object VMware.Vim.SelectionSpec
$sspecCluster.Name = "tspecCluster"
$sspecHost = New-Object VMware.Vim.SelectionSpec
$sspecHost.Name = "tspecHost"
# Traversal specs are needed to define how to get from one object to another
$tspecFolder = New-Object VMware.Vim.TraversalSpec
$tspecFolder.Type = "Folder"
$tspecFolder.Path = "childEntity"
$tspecFolder.Name = "tspecFolder"
$tspecFolder.SelectSet = @($sspecFolder, $sspecDatacenter, $sspecCluster)
$tspecDatacenter = New-Object VMware.Vim.TraversalSpec
$tspecDatacenter.Type = "Datacenter"
$tspecDatacenter.Path = "hostFolder"
$tspecDatacenter.Name = "tspecDatacenter"
$tspecDatacenter.SelectSet = @($sspecFolder)
$tspecCluster = New-Object VMware.Vim.TraversalSpec
$tspecCluster.Type = "ComputeResource"
$tspecCluster.Path = "host"
$tspecCluster.Name = "tspecCluster"
$tspecCluster.SelectSet = @($sspecHost)
$tspecHost = New-Object VMware.Vim.TraversalSpec
$tspecHost.Type = "HostSystem"
$tspecHost.Path = "configManager.storageSystem"
$tspecHost.Name = "tspecHost"
$ospec = New-Object VMware.Vim.ObjectSpec
$ospec.Obj = $sc.Content.RootFolder
$ospec.SelectSet = @($tspecFolder, $tspecDatacenter, $tspecCluster, $tspecHost)
# The property spec is neede to define what properties to retrieve
$pspec = New-Object VMware.Vim.PropertySpec
$pspec.Type = "HostStorageSystem"
$pspec.PathSet = @("multipathStateInfo", "storageDeviceInfo")
# The object spec defines the starting point of the retrievel (in this case - the root of the VC)
$spec = New-Object VMware.Vim.PropertyFilterSpec
$spec.ObjectSet = @($ospec)
$spec.PropSet = @($pspec)
$res = $pc.RetrieveProperties(@($spec))
# Construct the actual views
$views = @()
foreach ($item in $res) {
     $view = New-Object VMware.Vim.HostStorageSystem $sc.Client, $item.Obj
     # Assigning the properties is not needed if you aren't going to use them
     # (e.g. if you need the view just to invoke methods)
     $view.MultipathStateInfo = ($item.PropSet | where {$_.Name -eq "multipathStateInfo"}).Val
     $view.StorageDeviceInfo = ($item.PropSet | where {$_.Name -eq "storageDeviceInfo"}).Val
     $views += $view
}

This script will do a total of 3 server calls (2 for retrieving the ServiceInstance and PropertyCollector and one for "RetrieveProperties" call). You can skip the retrieval of the PropertyCollector by creating it in a similar way as the views in the end of the script (although I think this is completely unnecessary and won't bring much optimization).

You can find out more about the traversal specs (as well as any other server type) in the API reference: http://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/index.html

0 Kudos