AG-Status β SQL Availability Group Synchronization Monitor π Overview
The AG-Status PowerShell script is designed to monitor SQL Server databases that are part of Availability Groups (AGs). It verifies their synchronization state, provides clear console output, and sends email alerts for failures or successes.
This script is ideal for:
Daily health checks
Automated job/pipeline monitoring
Proactive alerting to reduce downtime
βοΈ Features
Checks all databases on configured SQL Server instances.
Evaluates whether databases are:
β Synchronized
β Out of Sync
Sends email alerts for both success and failure states.
Exits with clear status codes (0 = success, 1 = failure) for easy integration with monitoring systems.
π Requirements
PowerShell 5.1+ or PowerShell 7+
FailoverClusters module (included with Windows Server features)
SMTP relay (for sending notifications)
Custom function or module to check database sync state
The example uses Get-SITAGDatabaseStatus
You can replace this with your own logic (e.g., dbatools Get-DbaAgDatabase)
π οΈ Setup
Clone this repository or download the script.
git clone https://github.com//AG-Status.git cd AG-Status
Update script variables for your environment:
Import-Module path β point to your module or replace with your own logic.
$smtpServer β your SMTP relay.
$from β email sender address.
$to β recipient(s) for alerts.
Test the script manually:
.\AG-Status.ps1
Automate with Task Scheduler or DevOps pipeline:
Run on a schedule (e.g., every 15 minutes).
Use exit codes for CI/CD or monitoring pipelines.
π§ Email Alerts
Failure Email Subject: CRITICAL: SQL AG Databases Not Synchronized Body: List of affected databases with instance name and sync state.
Success Email Subject: SUCCESS: All SQL AG Databases Are Synchronized Body: Timestamp confirming successful synchronization.
π¦ Exit Codes Code Meaning Usage 0 All databases are synchronized Healthy state β no action required 1 One or more databases out of sync Critical issue β review failure email π§ Customization
Replace Get-SITAGDatabaseStatus with your own function or SQL query.
Adjust email configuration for SMTP authentication if required.
Expand notifications to include logging to file, Slack/Teams webhook alerts, or custom monitoring dashboards.
π Example Output
Console (Failure)
Critical: Found database(s) not synchronizing: SQL01 - SalesDB: Not in AG Group SQL02 - HRDB: Not Synchronized
Console (Success)
Success: All databases in Availability Groups are synchronized successfully as of 2025-06-06 14:30:12.
π License
MIT License β feel free to use and modify for your environment.
The following diagram shows how the script evaluates database synchronization status and handles alerts:
flowchart TD
A[Start Script] --> B[Collect DB Sync Status]
B --> C{Any DB Not in AG?}
C -- Yes --> D[Mark as Critical Issue]
C -- No --> E{Any DB Not Synchronized?}
E -- Yes --> D
E -- No --> F[All DBs Synchronized]
D --> G[Output Critical to Console]
G --> H[Send Failure Email]
H --> I[Exit 1]
F --> J[Output Success to Console]
J --> K[Send Success Email]
K --> L[Exit 0]