In some environments, administrators may need to verify whether two vCenter Servers have identical alarm definitions. This is useful during migration, consolidation, or validation tasks, as inconsistencies in alarms may lead to unexpected monitoring gaps.
This article provides a PowerCLI script that connects to two vCenter Servers, extracts all alarm definitions, and compares them to highlight any differences.
Use the provided PowerCLI script to connect to two vCenter Servers, retrieve their alarm definitions, and compare them. The output will display which alarms exist only in VC1 and which exist only in VC2.
# Note:
# 1. Replace $VC1, $VC2, $VC1_USER, $VC2_USER, $VC1_PASSWORD, $VC2_PASSWORD with actual vCenter information.
# 2. The script will disconnect any existing vCenter sessions before connecting to the specified servers.
# 3. The results will be displayed in the console, indicating alarms unique to each vCenter.
$VC1 = "FQDN or IP of vCenter 1"
$VC1_USER = "[email protected]"
$VC1_PASSWORD = "administrative credential of vCenter 1"
$VC2 = "FQDN or IP of vCenter 2"
$VC2_USER = "[email protected]"
$VC2_PASSWORD = "administrative credential of vCenter 2"
function Get-AlarmKey {
param($alarm)
$info = $alarm.ExtensionData.Info
if ($info.SystemName) { return "SYSTEM::$($info.SystemName)" }
elseif (-not [string]::IsNullOrWhiteSpace($alarm.Name)) { return "CUSTOM::$($alarm.Name)" }
else { return "ID::$($alarm.ExtensionData.MoRef.Value)" } # fallback to ID
}
# Disconnect existing connections, if any
if ($global:DefaultVIServers -and $global:DefaultVIServers.Count -gt 0) {
Disconnect-VIServer -Server * -Force -Confirm:$false
}
# Collect alarm definitions from VC1
Connect-VIServer -server $VC1 -user $VC1_USER -password $VC1_PASSWORD
$a = Get-AlarmDefinition
Disconnect-VIServer -Confirm:$false
# Collect alarm definitions from VC2
Connect-VIServer -server $VC2 -user $VC2_USER -password $VC2_PASSWORD
$b = Get-AlarmDefinition
Disconnect-VIServer -Confirm:$false
# Build comparable keys
$keysA = $a | ForEach-Object { Get-AlarmKey $_ }
$keysB = $b | ForEach-Object { Get-AlarmKey $_ }
# Compare
$onlyInA = Compare-Object $keysA $keysB -PassThru | Where-Object { $_.SideIndicator -eq '<=' }
$onlyInB = Compare-Object $keysA $keysB -PassThru | Where-Object { $_.SideIndicator -eq '=>' }
# Output results
$onlyInA | Sort-Object | ForEach-Object { "Only in VC ${VC1}: $_" }
$onlyInB | Sort-Object | ForEach-Object { "Only in VC ${VC2}: $_" }
When running the script, the console may display results similar to the following:
SYSTEM::... indicates a built-in system alarm that comes with vCenter.
CUSTOM::... indicates a user-created alarm.
The script compares only the presence of alarms (based on system name, custom name, or managed object reference ID).
It does not compare the internal alarm configuration or conditions (for example, expressions, triggers, or actions).
If two alarms have the same name but different conditions, they will appear as identical in this comparison.