I was looking on my blog for information to use the datastore information using the Horizon api’s but couldn’t find it so here’s a post on that.
This posts uses the soap api’s next time I’ll see what we can do with the REST api.
Index
- List by host or cluster
- List by Desktop or Farm
- List Datastore Clusters by host or cluster
- Get Datastore usage
- Calculate what datastore size is required
First I will make a connection like I always do
Now let’s see what methods are available under the Datastore service
Let’s start with the easy 3 first aka the bottom ones
[sta_anchor id=”datastore_listdatastoresbyhostorcluster” unsan=”Datastore_ListDatastoresByHostOrCluster” /]
Datastore_ListDatastoresByHostOrCluster
The name says enough with Datastore_ListDatastoresByHostOrCluster you are able to list datastores using the HostOrClusterID.
I am cutting some corners here how to find this out but to get this HostOrClusterID we need to get the DatacenterId and to get that we’ll need the VirtualcenterId.
To get all virtualcenters in a pod you need to use virtualcenters_list() and what I do in this example is listing them first and than putting the first virtualcenter in an variable.
$hvservice.VirtualCenter.VirtualCenter_List() $VC=$hvservice.VirtualCenter.VirtualCenter_List() | Select-Object -first 1
and the same for the datacenter using the virtualcenterID
$hvservice.Datacenter.Datacenter_List($vc.id) $DC=$hvservice.Datacenter.Datacenter_List($vc.id) | Select-Object -first 1
With the datacenter ID I’ll retreive the info under HostOrCluster and store it in an variable.
$hvservice.HostOrCluster.HostOrCluster_GetHostOrClusterTree($dc.id) $tree=$hvservice.HostOrCluster.HostOrCluster_GetHostOrClusterTree($dc.id)
Let’s browse this object and see what we can find
We can clearly see the name here and as I need Cluster_Pod2 I am putting that one in an object
$pod2cluster=$tree.TreeContainer.Children.info | select-object -last 1 $pod2cluster
And with this object I can get to my datastores and again I store them in an object
$hvservice.Datastore.Datastore_ListDatastoresByHostOrCluster($pod2cluster.id) $datastores=$hvservice.Datastore.Datastore_ListDatastoresByHostOrCluster($pod2cluster.id)
Let’s see what’s in there
So we see most of the basic info in here that we might need including name, capacity and free space. Not sure why the numberofvm’s is empty as all of them have vm’s.
[sta_anchor id=”datastore_listdatastoresbydesktoporfarm” unsan=”Datastore_ListDatastoresByDesktopOrFarm” /]
Datastore_ListDatastoresByDesktopOrFarm
Let’s see what we need for this one
So an object is needed of the type VMware.Hv.DatastoreSpec let’s define the object and see what’s in it.
As I am not 100% sure if all are required or not and what might break I’ll have a look at the API explorer article of this.
So it requires either a DesktopID OR a FarmID wile you can provide the hostorclusterId but that will be populated if you don’t provide one.
I am not going to build the query here to get a desktop pool so I’ll just use get-hvpool and get-hvfarm from the vmware.hv.helper powershell module.
Next I put the $pool.id in the spec and get the details
$spec.DesktopId=$pool.id $hvservice.Datastore.Datastore_ListDatastoresByDesktopOrFarm($spec) $datastores=$hvservice.Datastore.Datastore_ListDatastoresByDesktopOrFarm($spec) $datastores.datastoredata
So this lists all the datastores that I have available in this cluster. I know this 100% sure as the ISO datastore is a read-only datastore that doesn’t have any desktops.
Let’s do the same using the farmId
$spec.DesktopId=$null $spec.FarmId=$farm.id $datastores=$hvservice.Datastore.Datastore_ListDatastoresByDesktopOrFarm($spec) $datastores
Same amount of datastores so the same result.
[sta_anchor id=”datastore_listdatastoreclustersbyhostorcluster” unsan=”Datastore_ListDatastoreClustersByHostOrCluster” /]
Datastore_ListDatastoreClustersByHostOrCluster
As I don’t have any datastore clusters in my lab I cannot show it but you’ll need the same hostorclusterid as we used for Datastore_ListDatastoresByHostOrCluster
[sta_anchor id=”datastore_getusage” unsan=”Datastore_GetUsage” /]
Datastore_GetUsage
This method shows what desktop pools are using a particular datastore. When doing a dry run it shows that a DatastoreId is needed.
I will use one of the items that I still have stored in my $datastores variable
$datastore=$datastores |Select-Object -last 1 $datastore.DatastoreData $hvservice.Datastore.Datastore_GetUsage($datastore.id)
So this ia a rather boring datastore as it only has 1 pool configured to use it (and it doesn’t even have any vm’s from this pool on it) but you’ll see that there is another datastore configured for this pool as wel. I do have a more used datastore though on a local nvme drive.
$datastore=$datastores | where {$_.datastoredata.name -like "*nvme*"} $hvservice.Datastore.Datastore_GetUsage($datastore.id)
As you can see it shows the desktop pools and even the single farm I have that use this datastore each with their own disk usage.
[sta_anchor id=”datastore_getdatastorerequirements” unsan=”Datastore_GetDatastoreRequirements” /]
Datastore_GetDatastoreRequirements
The Datastore_GetDatastoreRequirements method does a calculation of what disk space might be needed for a desktop pool.
So let’s see what we need
$reqspec=new-object VMware.Hv.DatastoreRequirementSpec $reqspec
That’s a lof and as a screenshot wouldn’t fit here is the link to the APi explorer page on it: here
To fill these things I will use the $pool variable that I still have stored.
$reqspec.DesktopId=$pool.id $reqspec.Source="INSTANT_CLONE_ENGINE" $reqspec.VmId=$pool.AutomatedDesktopData.VirtualCenterProvisioningSettings.VirtualCenterProvisioningData.parentvm $reqspec.SnapshotId=$pool.AutomatedDesktopData.VirtualCenterProvisioningSettings.VirtualCenterProvisioningData.Snapshot $reqspec.PoolSize=30 $hvservice.Datastore.Datastore_GetDatastoreRequirements($reqspec)
And when I change the poolsize