Execution Flow
Primary only (v1.9.152+): AGs in which the instance is a secondary are skipped and logged together with the actual primary. Removing and re-adding a database only works on the primary; on a secondary,
Remove-DbaAgDatabase would take the local copy out of the AG and the re-add would fail. If the instance is primary of no AG, nothing is changed. Run the job on every node and each node repairs the AGs it is currently primary of, which keeps working after a failover.Calls
Invoke-sqmSqlAlwaysOnAutoseeding for those AGs first to ensure AutoSeed is enabled before any repair. Each repair action (remove, drop, add) is written to the Windows Event Log source sqmAlwaysOn (created automatically). Use -NoReport to suppress event-log output in scheduled-job contexts.Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| -SqlInstance | String | Optional | Primary SQL instance. Default: $env:COMPUTERNAME. |
| -SqlCredential | PSCredential | Optional | SQL authentication credential. |
| -Force | Switch | Optional | Also repair databases that are already considered healthy (force refresh). |
| -NoReport | Switch | Optional | Suppress Windows Event Log writes (intended for scheduled-job execution). |
| -EnableException | Switch | Optional | Throw terminating errors instead of logging warnings. |
| -WhatIf / -Confirm | Switch | Optional | Standard ShouldProcess support. |
Return Value
Returns a PSCustomObject[], one entry per repaired (or skipped) database, with: SqlInstance, DatabaseName, AvailabilityGroup, Status (RepairSuccess, RepairFailed, RepairSkipped, GlobalError), Message.
Examples
Example 1, Repair all unhealthy AG databases on local instance
Repair-sqmAlwaysOnDatabasesExample 2, Force repair of all databases including healthy ones
Repair-sqmAlwaysOnDatabases -SqlInstance "SQL01" -ForceExample 3, Preview without changes
Repair-sqmAlwaysOnDatabases -SqlInstance "SQL01" -WhatIf