I was recently helping out my friend Paudie O'Riordan with a request from a customer who was looking for a way to collect detailed space utilization for their VMs (VM Home, VMDK & swap) running on VSAN. Today, this level of granularity is not available in the vSphere UI and the customer was interested in both the used and reserved capacity on a per-VM basis. Luckily, this information can be retrieved using the VSAN Management API.
To do so, we just need to use the VsanQueryObjectIdentities() API method, which I have used in the past to retrieve things like "thick" provisioned VM and translating VSAN Object IDs to their friendly VM display name. To retrieve space utilization information, we just need to set the includeSpaceSummary property to be true. While developing the PowerCLI sample script, I found that this specific property is currently not supported when querying vCenter Server and to retrieve this information, you must go directly to each ESXi host within the VSAN Cluster. I have already filed a feature request and it looks like this will be fixed in a future vSphere release.
In the meantime, we still have a solution in which we can use vCenter Server to provide us with a list of ESXi hosts within the VSAN Cluster and given ESXi host credentials, connect to each host using the VSAN API and aggregating the results as if it was done at the VSAN Cluster level. With that, I have created the VSANVMDetailedUsage.ps1 which includes a simple function called Get-VSANVMDetailedUsage which accepts the name of VSAN Cluster as well as an optional VM name for additional filtering. As mentioned earlier, you will need to provide valid credentials to your ESXi hosts (read-only is sufficient) and I have a check in place to ensure the following two variables have been set within the script before it proceeds:
$ESXiHostUsername= ""
$ESXiHostPassword = ""
Here is an example of running the script with just the VSAN Cluster name and no VM filtering, it will retrieve ALL VMs within the VSAN Cluster:
Get-VSANVMDetailedUsage -Cluster "VSAN-Cluster"
Here is an example of running the script with VSAN Cluster name and filtering on a specific VM:
Get-VSANVMDetailedUsage -Cluster "VSAN-Cluster" -VM "Ubuntu-SourceVM"
Dave Perella says
William, 1st love your posts always seem to find solutions to the oddball issues we are having. So thanks we appreciate you sharing. So to the nitty-gritty; we have script that collects vmdk sizes per VM for "costing"...etc...the line of code we use to capture this information from our SAN connected disks is:
- @{Name="Storage_GB_VMPremium"; Expression={[int]($($_.LayoutEx.File|?{$_.name -like "*-flat.vmdk"}|?{($_.name -like '*-VMP-*')}|%{$_.size}|measure-object -Sum).sum/1024/1024/1024)}}, `
The datastores have -VMP- in the name the result is the size of the disk in an appropriately named column. However, when we try that same line of code with our VSAN datastores:
- @{Name="Storage_GB_vSAN"; Expression={[int]($($_.LayoutEx.File|?{$_.name -like "*-flat.vmdk"}|?{($_.name -like '*-VSAN*')}|%{$_.size}|measure-object -Sum).sum/1024/1024/1024)}}, `
The result is zeros across the board. I have a couple ideas but can't find any information on what I need to do to get the result I need.
Any help you can provide would again be greatly appreciated.
Dave
thementor666 says
Hi Dave i think the reason you cant do select on the result is because in William's function the results are being piped to Format-Table
if you remove the pipe to Format-Table and just return result i think what you're trying to do will work.
-DM
Daniel Burtescu says
Hi William, will your script take in consideration the size of vm files after or before the vsan policy is applied? If a vmdk has a used size of 100 GB and we apply a raid6 policy will the script report 100 GB or 150 GB?
William Lam says
The property I'm keying off of is called physicalUsedGB, so it should be whats actually being consumed. Should be simple enough test for you to try out 🙂
Louise says
Hi William, we ran into an error while running the code
Exception calling "VsanQueryObjectIdentities" with "6" argument(s): "'NoneType' object has no attribute '_moId'"
At C:\Users\Administrator\Documents\VSANVMDetailedUsage02.ps1:54 char:9
+ $identities = $vos.VsanQueryObjectIdentities($null,$null,$nul ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FaultException
ConvertFrom-Json : Cannot bind argument to parameter 'InputObject' because it is null.
At C:\Users\Administrator\Documents\VSANVMDetailedUsage02.ps1:56 char:37
+ $json = $identities.RawData|ConvertFrom-Json
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [ConvertFrom-Json], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertFromJsonCommand
Any help you can provide would again be greatly appreciated.
Roshan says
Hi William,
My customer needs to get the detailed per VM VSAN usage and your script perfectly work. But based on customers security policy they cant have same password for all ESXi host. Therefore we need to create a separate user for connecting to ESXi host with the same password. So what are the permissions should be given to this user to get the same result. We have tried with a user with Read-Only role and it failed.
Miguel says
Hello William, I am trying to use your script, but I have vcenter 7.0.U3o under VCF scheme, in a test environment I generated a local user since all ESX have a different password, but I had an error that was thrown indicating that it is not Find the vSAN cluster that I am specifying, do you think it is possible to tell me why this is or if it is no longer compatible?
William Lam says
Did you typo the vSAN cluster name? As the blog post outlines, it needs to connect to vCenter Server endpoint first AND then it reaches out to individual ESXi hosts. So if it can't find the vSAN Cluster, either it doesn't exists or you didn't specify the correct name