Site icon Digital Thought Disruption

Monitoring and Health Reporting with PowerCLI: VMs, Hosts, Snapshots, and Orphaned Objects

Introduction

Proactive monitoring is key to reducing incidents and downtime. PowerCLI makes it easy to schedule and automate health checks, track configuration drift, and alert on silent risks such as orphaned snapshots or VMs.

In this article, you will learn how to:


My Personal Repository on GitHub

VMware Repository on GitHub


Health Check Report: Overview

Start by collecting data across core inventory components:

$report = @()

Get-VM | ForEach-Object {
$report += [PSCustomObject]@{
Name = $_.Name
PowerState = $_.PowerState
VMTools = $_.ExtensionData.Guest.ToolsStatus
Host = $_.VMHost.Name
SnapshotAge = ($_ | Get-Snapshot | Sort-Object Created -Descending | Select-Object -First 1).Created
}
}

$report | Export-Csv "C:\Reports\VM_HealthReport.csv" -NoTypeInformation

Snapshot Auditing

Find VMs with Snapshots Older Than 7 Days

Get-VM | Get-Snapshot | Where-Object {$_.Created -lt (Get-Date).AddDays(-7)} | Select VM, Name, Created

Remove All Snapshots Older Than 14 Days

Get-VM | Get-Snapshot | Where-Object {$_.Created -lt (Get-Date).AddDays(-14)} | Remove-Snapshot -Confirm:$false

Identify Disconnected or Invalid VMs

Orphaned VMs

Get-VM | Where-Object {$_.PowerState -eq "Unknown"}

VMs with Invalid or Missing Files

Get-VM | Where-Object {$_.ExtensionData.Summary.Runtime.ConnectionState -eq "invalid"}

Diagram: Report Automation Flow


Scheduled Daily Report Script

Save this script and use Task Scheduler or cron to run it each morning.

$report = Get-VM | Select Name, PowerState, @{N="ToolsStatus";E={$_.ExtensionData.Guest.ToolsStatus}}, @{N="SnapshotCount";E={($_ | Get-Snapshot).Count}}, VMHost

$timestamp = Get-Date -Format "yyyy-MM-dd"
$report | Export-Csv -Path "C:\Reports\Daily_VM_Report_$timestamp.csv" -NoTypeInformation

Use Case: Compliance Snapshot Audit Before Change Windows

Before patching or upgrades, ensure that:

Use this PowerCLI one-liner:

Get-VM | Where-Object {
($_.PowerState -eq "PoweredOn") -and
(($_ | Get-Snapshot).Count -eq 0) -and
($_.ExtensionData.Guest.ToolsStatus -eq "toolsOk")
}

Add-on: HTML Reporting (Optional)

Convert reports to HTML for email or dashboard use.

$report = Get-VM | Select Name, PowerState, VMHost
$report | ConvertTo-Html -Title "VM Summary Report" | Out-File "C:\Reports\VM_Summary.html"

Troubleshooting Report Failures

IssueFix
Snapshot command fails on some VMsCheck for permissions or backup locks
ToolsStatus returns blankEnsure VM is powered on and Tools are installed
PowerCLI script does not export fileConfirm write permissions and file path exists
Email or automation integration failsUse PowerShell Send-MailMessage with proper SMTP credentials

What’s Next

In the next article, we will explore:

Exit mobile version