WilliamLam.com

  • About
    • About
    • Privacy
  • VMware Cloud Foundation
  • VKS
  • Homelab
    • Resources
    • Nested Virtualization
  • VMware Nostalgia
  • Apple
You are here: Home / Automation / Retrieving detailed per-VM space utilization on VSAN

Retrieving detailed per-VM space utilization on VSAN

06.27.2018 by William Lam // 8 Comments

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"

More from my site

  • How to move vSAN Datastore into a Folder?
  • How to tell if an ESXi host is a VSAN Witness Virtual Appliance programmatically?
  • VSAN Flash/MD capacity reporting
  • VSAN vCheck Plugins
  • Quick Tip - Retrieving vSAN File Share Network Permissions using vSAN API & PowerCLI

Categories // Automation, PowerCLI, VSAN Tags // PowerCLI, VSAN, vSphere API

Comments

  1. *protectedDave Perella says

    10/10/2018 at 4:20 am

    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

    Reply
    • *protectedthementor666 says

      01/14/2019 at 1:41 am

      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

      Reply
  2. *protectedDaniel Burtescu says

    01/31/2019 at 3:36 pm

    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?

    Reply
    • William Lam says

      02/01/2019 at 4:31 am

      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 🙂

      Reply
  3. *protectedLouise says

    09/17/2019 at 11:42 pm

    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.

    Reply
  4. *protectedRoshan says

    09/23/2019 at 12:25 pm

    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.

    Reply
  5. *protectedMiguel says

    03/05/2024 at 9:42 am

    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?

    Reply
    • William Lam says

      03/05/2024 at 1:44 pm

      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

      Reply

Leave a Reply to William LamCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Search

Thank Author

Author

William is Distinguished Platform Engineering Architect in the VMware Cloud Foundation (VCF) Division at Broadcom. His primary focus is helping customers and partners build, run and operate a modern Private Cloud using the VMware Cloud Foundation (VCF) platform.

Connect

  • Bluesky
  • Email
  • GitHub
  • LinkedIn
  • Mastodon
  • Reddit
  • RSS
  • Twitter
  • Vimeo

Recent

  • VMware Flings is now available in Free Downloads of Broadcom Support Portal (BSP) 05/19/2025
  • VMUG Connect 2025 - Minimal VMware Cloud Foundation (VCF) 5.x in a Box  05/15/2025
  • Programmatically accessing the Broadcom Compatibility Guide (BCG) 05/06/2025
  • Quick Tip - Validating Broadcom Download Token  05/01/2025
  • Supported chipsets for the USB Network Native Driver for ESXi Fling 04/23/2025

Advertisment

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

Copyright WilliamLam.com © 2025

 

Loading Comments...