top of page

PowerCLI Automation Beyond Snapshots

  • markjramos
  • Apr 24
  • 3 min read

If you have been using PowerCLI primarily for snapshot management, you are barely scratching the surface of what is possible. PowerCLI is one of the most powerful tools in a VMware administrator's toolkit, and investing time in it pays dividends across every area of vSphere operations. This post covers practical automation scripts beyond the basics — from VM inventory reporting to host health checks and storage management.

Getting Started: Connecting to vCenter

Before running any script, connect to your vCenter Server. Use Connect-VIServer -Server vcenter.yourdomain.com and authenticate with your credentials. For automation pipelines, store credentials securely using New-VICredentialStoreItem rather than embedding plaintext passwords in scripts. Always test scripts against a non-production environment first, and use the -WhatIf or -Confirm parameters where available to preview changes before applying them.

VM Inventory and Reporting

A comprehensive VM inventory report is one of the most requested scripts in any VMware environment. The following approach retrieves all VMs across your vCenter and exports key properties to CSV. Use Get-VM to retrieve all virtual machines, then use Select-Object to pull Name, PowerState, NumCpu, MemoryGB, the cluster name via Get-Cluster, and the datastore via Get-Datastore. Pipe the result to Export-Csv -Path C:\Reports\VMInventory.csv -NoTypeInformation. Schedule this script weekly to maintain an up-to-date asset inventory without manual effort.

Host Health Check Script

Checking host health across a large cluster manually is time-consuming. With PowerCLI, you can automate this into a daily report. Use Get-VMHost to retrieve all hosts, then check ConnectionState, PowerState, and OverallStatus. For each host, retrieve the health status of hardware sensors using Get-VMHostHardware. Flag any hosts where OverallStatus is not Green and output those to a separate alert CSV. You can extend this script to send an email alert using Send-MailMessage when issues are detected, giving you an automated early warning system for host problems.

Datastore Usage and Capacity Reporting

Running out of datastore space is a preventable problem when you have proper monitoring in place. Use Get-Datastore to retrieve all datastores, then calculate the FreeSpaceGB and CapacityGB values, and compute a UsedPercent field. Filter datastores where UsedPercent exceeds 80 percent and export the results. This script is particularly useful in environments where multiple teams provision workloads and capacity management is decentralized. Running it nightly and reviewing the output takes five minutes and can prevent emergency storage expansions.

Bulk VM Configuration Changes

One of the biggest time-savers in PowerCLI is bulk configuration changes. Need to add a note to all VMs in a specific folder? Use Get-VM -Location (Get-Folder 'FolderName') to scope the query, then pipe to Set-VM -Notes 'Your note here' -Confirm:$false. Need to change the CPU count on a set of powered-off VMs matching a naming convention? Use Get-VM -Name 'Pattern*' | Where-Object {$_.PowerState -eq 'PoweredOff'} | Set-VM -NumCpu 4. Operations that would take hours in the vSphere client take seconds in PowerCLI.

Scheduled Tasks and Automation Pipelines

For scripts you run regularly, schedule them using Windows Task Scheduler or integrate them into a CI/CD pipeline using PowerShell Core and a tool like Jenkins or GitLab CI. Store your scripts in a Git repository with proper version control, use environment variables for credentials rather than hardcoding them, and add logging to each script so you have an audit trail of automated actions. As your automation library grows, organizing scripts into a PowerShell module with proper function naming and comment-based help makes them reusable and shareable across your team.

PowerCLI is a skill that compounds over time. Every script you write becomes a building block for the next one. Start with the reporting scripts, get comfortable with the object pipeline, and then move into configuration automation. Before long, routine administration tasks that used to consume hours of your week will run unattended. Check out the companion post on the VMware Snapshot Cleanup Script for another practical automation example.

Comments


©2022 by virtualtechblog. Proudly created with Wix.com

bottom of page