If you work with VMware, you know how annoying it can be to click through the vSphere UI just to check some basic VM details. That’s where PowerCLI comes in handy. With a few lines of PowerShell, you can quickly pull up CPU, RAM, and power state info for all your VMs. In this post, I’ll show you a script that connects to vCenter, grabs VM details, and logs out – all in one go.
Prerequisites
Before running this script, make sure you have:
- VMware PowerCLI installed.
- Proper credentials to access your vCenter Server.
- PowerShell script execution enabled.
The PowerShell Script
Here’s a quick script to fetch details of all VMs in vCenter:
# Import VMware PowerCLI module
Import-Module VMware.PowerCLI
Import-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue
Set-PowerCLIConfiguration -ProxyPolicy NoProxy -DefaultVIServerMode multiple -Scope User -InvalidCertificateAction ignore -Confirm:$false | Out-Null
# Define vCenter Server and credentials
$vCenterServer = "your-vcenter-server"
$username = "administrator@vsphere.local"
$password = "your-password"
# Connect to vCenter Server
Connect-VIServer -Server $vCenterServer -User $username -Password $password
# Get all VMs from vCenter
$vms = Get-VM
# Loop through each VM and retrieve its configuration
foreach ($vm in $vms) {
if ($vm) {
# Get CPU and RAM configuration
$cpuCount = $vm.NumCpu
$ramGB = $vm.MemoryGB
$coresPerSocket = $vm.ExtensionData.Config.Hardware.NumCoresPerSocket
$socketCount = $cpuCount / $coresPerSocket
$autoCorePerSocket = $vm.ExtensionData.Config.Hardware.AutoCoresPerSocket
$powerState = $vm.PowerState
# Output the details
Write-Host "VM Name: $($vm.Name)"
Write-Host "Power State: $powerState"
Write-Host "CPU Count: $cpuCount"
Write-Host "RAM: $ramGB GB"
Write-Host "Cores per Socket: $coresPerSocket"
Write-Host "Socket Count: $socketCount"
Write-Host "Auto Core Per Socket: $autoCorePerSocket"
Write-Host "----------------------------------------"
}
}
# Disconnect from vCenter Server
Disconnect-VIServer -Server $vCenterServer -Confirm:$false
What this script does
- Loads PowerCLI modules: Ensures all necessary VMware cmdlets are available.
- Logs into vCenter: Connects using provided credentials.
- Gets all VMs: No need to specify names manually.
- Loops through each VM: Retrieves CPU, RAM, and power state.
- Prints results: Displays everything in the console.
- Logs out: Closes the session to keep things clean.
Why use this script?
- No more clicking through vSphere UI: Get all VM details instantly.
- Scales easily: Works for any number of VMs.
- Saves time: Run once and get everything you need.
Final thoughts
This script is a must-have for any VMware engineer who wants a fast and easy way to check VM configurations. Whether you’re troubleshooting, auditing, or just curious about your VM resources, PowerCLI makes it simple. Try it out and let me know if you have any improvements! 🚀