Menu
vChamp
  • About
  • Community
  • VCF Suite
    • VCF Automation
    • VCF Operations
    • VCF Operations – Log Management
    • VCF Automation Orchestrator
  • Scripts
  • Tags
  • vSphere
vChamp

PowerCLI: Get List of VMs with Resource Reservations Set

Posted on July 30, 2023April 21, 2025 by Don Horrox

Estimated reading time: 8 minutes

One of my first PowerCLI use cases surfaced from a seemingly basic question: Which virtual machines in our inventory have resource reservations configured? Although resource reservations serve an important purpose, especially for critical virtual appliances, they can easily become a nuisance if accidentally configured in a VM template. Unfortunately, there is no straightforward way to view this information en masse using the vCenter web UI. What a tremendous opportunity to explore PowerCLI!

Our script will allow us to export the results table in CSV format or in a PowerShell table for one-time viewing. This is especially handy for cases where you need information quickly and do not wish to bother with exported files.

Before you begin

This article assumes you already have PowerCLI installed and the user credentials you intend to supply have adequate permissions to the target vCenter server.

Prepare the Environment

  1. Download the GET_VM_Properties_Resource_Reservation.ps1 script from my GitHub repository and save to a local directory of your choice.
  2. Launch a PowerShell session. Depending on user permissions, you may need to launch PowerShell with elevation (as Administrator).

Execute Script

  1. Change working directories to the directory containing the script (i.e., C:\Scripts)
    cd C:\Scripts
  2. Execute the script by typing or pasting the below line within the PowerShell window.
    .\GET_VM_Properties_Resource_Reservation.ps1

    Note: You can use autocomplete to populate the name of the file if you type the first few letters. Try typing “.\GET” without quotes and press the Tab key to give it a try!
  3. The “Main Menu” is presented, prompting the user to specify the desired output format. Recall that the options are either “CSV File” or “PowerShell Table“. Type the number which corresponds with the desired output, then press the Enter key.

  4. The next prompt asks for the Fully Qualified Domain Name (FQDN) of the vCenter server. Type the vCenter FQDN, then press the Enter key.
  5. The next prompt asks for the User ID which should be used to query the vCenter server. Type the desired User ID, then press the Enter key. Predictably, the password field is the next prompt: type the corresponding password and press the Enter key to execute the query.


    Note: User accounts which are child objects of the same SSO Domain as the vCenter server are not required to specify a prefix or suffix to the User ID. However, accounts which are child objects of other SSO domains will be required to specify one or the other, depending on the LDAP configuration. For example, let us assume that the vCenter server’s default SSO Domain is vcenter.local. If the user account is a child object of the vcenter.local SSO Domain, then you would simply enter the user ID (“jdoe” for example) rather than the user ID and suffix (“[email protected]” for example). In similar fashion, if the vCenter server’s default SSO domain is vcenter.local but also has LDAP configured for XYZ Corp’s SSO Domain (i.e., “xyzcorp.com”), a user account which is a child object of the XYZ Corp domain would either specify a NETBIOS domain prefix (i.e., “xyz”) before the user ID, or the domain suffix (i.e., “@xyzcorp.com”) after the user ID. The prefix/suffix may vary based on the LDAP and NETBIOS configuration in your environment. The point being, you would only specify the SSO Domain prefix or suffix if the user account belongs to a SSO Domain other than what is selected as the default for the vCenter server.
  6. The query will now execute, which can take a moment (possibly 2-3 minutes) depending on several factors such as resource availability on the vCenter server, number of virtual machines, etc.

  7. Based on the output format selected in prior steps, the script will either export the query results to a CSV file or display a PowerShell table. If the CSV output was selected, you will find it in the same directory as the script.
  8. The user session between PowerShell/PowerCLI and the target vCenter server will automatically disconnect at the end of the script.

There you have it! We have successfully leveraged a script to obtain details which are otherwise challenging to obtain en masse using the vCenter GUI.

Behind the Scenes

How did the script obtain this information and format accordingly? We will investigate a bit more closely below:

Lines 1-18
This is your average introductory comment block. Although useful when referencing the code directly, there is no functional value to this block.

<#
.SYNOPSIS
Get VMs with Resource Reservations
Intended to query vCenter to retrieve a list of VM's which have CPU/Memory Reservations set and export results to CSV or PS Grid.

Author: Don Horrox (vChamp - https://www.vchamp.net)
Version: 1.0

.DESCRIPTION
 This script must be run on a system with either the VMware PowerCli snap-in or module and an
 account that has appropriate rights to connect to vCenter using PowerCLI.
 To install the PowerCLI module run
 Install-Module -Name VMware.PowerCLI
 Install-Module -Name VMware.PowerCLI –Scope CurrentUser

.NOTES
N/A
#>
PowerShell

Lines 20-24
We begin by configuring a PowerShell function for timekeeping. This will help record the date and time for each line of our log file. We then set the location where the log should be saved, which is the same directory as the script in this case.

## Prepare Logging
function Get-TimeStamp {
    return "[{0:MM/dd/yy}  {0:HH:mm:ss}]" -f (Get-Date)
}
$outputLog = ".\get_vm_properties_reservation.log"
PowerShell

Lines 26-64
The first line is written to our log file to indicate that a new session has started. We then configure the appearance and corresponding values for the “Main Menu” which is presented to the user.

## Initialize Script
Write-Output "$(Get-Timestamp) Initializing script." | Out-File $outputLog -Append

# Main Menu
function Show-Menu
{
    param (
        [string]$Title = 'Get VMs with Resource Reservations'
    )
    Clear-Host
    Write-Host "================ $Title ================"
    Write-Host "Select your desired output format:"
    Write-Host "`n"
    Write-Host "1) CSV File"
    Write-Host "`n"
    Write-Host "2) PowerShell Table"
    Write-Host "`n"
    Write-Host "Q: Press 'Q' to quit."
    Write-Host "`n"
    Write-Host "`n"
}
Write-Output "$(Get-Timestamp) Waiting for user input." | Out-File $outputLog -Append

# Main Menu: Action
     Show-Menu -Title 'Main menu'
     $selection = Read-Host "Please make a selection"
     switch ($selection)
     {
         '1' {
            $OutputSelection = "CSV"
         } '2' {
             $OutputSelection = "PowerShell Table"
            } 'q' {
                $OutputSelection = "Exit"
                Write-Output "$(Get-Timestamp) User aborted script. Exiting." | Out-File $outputLog -Append
                Exit
            }
        }
        Write-Output "$(Get-Timestamp) User selected $OutputSelection." | Out-File $outputLog -Append
PowerShell

Lines 66-74
Fields are provided for the user to specify the vCenter server FQDN, User ID, and Password. Once provided, the script will attempt to authenticate with the vCenter server.

# Connect to vCenter
Clear-Host
Write-Host "$Outputselection output selected." -ForegroundColor Yellow
Write-Output "$(Get-Timestamp) Waiting for credentials." | Out-File $outputLog -Append
$VCServer = Read-Host "Enter your vCenter FQDN"
$Username = Read-Host "Enter your username"
$Password = Read-Host "Enter password" -AsSecureString
$null = Connect-VIServer $VCServer -User $username -Password ([Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)))
Write-Output "$(Get-Timestamp) Connecting to $VCServer as $Username ." | Out-File $outputLog -Append
PowerShell

Lines 76-86 The query is executed on the vCenter server without filter, therefore all virtual machines are in-scope. Virtual Machines with a CPU and/or Memory reservation will be written to memory for use in the next code block.

# Query vCenter for VM's with a resource reservation
Write-Host "Executing query." -ForegroundColor Yellow
Write-Output "$(Get-Timestamp) Executing query." | Out-File $outputLog -Append
$Report = @()
$VMs = Get-VM | Where-Object {$_.ExtensionData.ResourceConfig.MemoryAllocation.Reservation -ne "0" -or $_.ExtensionData.ResourceConfig.CpuAllocation.Reservation -ne "0"} | Sort-Object -Property Name
ForEach ($VM in $VMs)
    { 
    $Report += "" | Select @{N="Name";E={$VM.Name}},
    @{N="CPU Reservation";E={$VM.ExtensionData.ResourceConfig.CpuAllocation.Reservation}},
    @{N="Memory Reservation";E={$VM.ExtensionData.ResourceConfig.MemoryAllocation.Reservation }} 
    }
PowerShell

Lines 88-95
Depending on the output format selected prior, the script will either export the query results to a CSV file or present a PowerShell table. Only VMs which are configured with a CPU and/or Memory reservation are displayed.

# Output
if ($OutputSelection -eq "Powershell Table") {
    $report | Out-GridView
    Write-Output "$(Get-Timestamp) Presenting PowerShell Table to user." | Out-File $outputLog -Append }
    
    if ($OutputSelection -eq "CSV") {
    $report | Export-Csv -Path .\VM_Reservations.csv -NoTypeInformation
    Write-Output "$(Get-Timestamp) Exporting to CSV." | Out-File $outputLog -Append }
PowerShell

Lines 97-103
Upon completion, the script will disconnect the user session with the vCenter server.

# Log out of PowerCLI
Write-Host "Disconnecting vCenter session." -ForegroundColor Yellow
Write-Output "$(Get-Timestamp) Disconnecting from $VCServer ." | Out-File $outputLog -Append
Disconnect-VIServer -Server $VCServer -Force -Confirm:$false
Write-Host "Session disconnected." -ForegroundColor DarkGreen
Write-Output "$(Get-Timestamp) vCenter session disconnected." | Out-File $outputLog -Append
Write-Host "Query complete. Please close terminal window." -ForegroundColor Green
PowerShell

That wasn’t too painful, right? I hope this post has been meaningful to you!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Don Horrox

Virtualization professional and enthusiast.


VCP VCF 9 Architect Badge VCP VCF 2024 Badge VCP CMA 2024 Badge
vExpert Badge
vExpert VCF Subprogram Badge

Connect

  • GitHub
  • RSS Feed
  • LinkedIn
  • X

Recent Posts

  • VMware Explore 2025 – That’s a Wrap!September 8, 2025
  • VMware Explore 2025 – Presenting!July 26, 2025
  • Know before you go: Explore 2025 in Las VegasJuly 2, 2025
© 2023 - 2025 Don Horrox | All Rights Reserved