Compare-sqmAlwaysOnLogins
Always On
sqmSQLTool v1.8.2+ · Compare
✓ -WhatIf supported
Output is returned as a
[PSCustomObject] collection and written as a TXT + HTML report (auto-opened unless -NoOpen).
Use -FailOnDrift to let a SQL Agent job step fail automatically when drift is detected — the function then writes Windows Event 9001 and throws an exception.
The
sa account (identified by SID 0x01, regardless of rename) is excluded by default because each instance intentionally uses its own random password that is never synchronized.
Diagnostic counterpart to Sync-sqmLoginsToAlwaysOn.
Parameters
| Parameter | Type | Required | Default | Notes |
|---|---|---|---|---|
| -SqlInstance | string | Optional | $env:COMPUTERNAME | Entry point — Primary or any Secondary of the AG. |
| -AvailabilityGroupName | string | Optional | — (first AG found) | Name of the AG to compare. If omitted, the first AG found is used; a warning is issued when multiple AGs exist. |
| -SqlCredential | PSCredential | Optional | — | Used for all replica connections. |
| -IncludeSystemLogins | switch | Switch | $false | Also compare sa, ##MS_*, NT SERVICE\*, NT AUTHORITY\*, BUILTIN\*. When set, sa password-hash drift is reported (usually Critical). |
| -Login | string[] | Optional | @() = all | Include only these logins. Wildcards allowed. |
| -ExcludeLogin | string[] | Optional | @() | Exclude these logins. Wildcards allowed. |
| -OnlyDifferences | switch | Switch | $false | Return and report only logins with status Warning or Critical. |
| -OutputPath | string | Optional | Get-sqmDefaultOutputPath | Target directory for TXT and HTML report files. |
| -NoOpen | switch | Switch | $false | Suppress automatic opening of the HTML report. |
| -FailOnDrift | switch | Switch | $false | Write Windows Event 9001 (Source: sqmSQLTool) and throw when any Warning or Critical logins exist. Designed for SQL Agent job steps. Implies -NoOpen. |
| -ContinueOnError | switch | Switch | $false | Log errors and continue instead of re-throwing. |
| -EnableException | switch | Switch | $false | Throw immediately on error (dbatools-style). |
| -WhatIf / -Confirm | switch | Optional | — | ShouldProcess guard on the report-writing step. Skips TXT/HTML output in -WhatIf mode. |
Execution Flow
Examples
Compare all logins on the first AG found on SQL01
Compare-sqmAlwaysOnLogins -SqlInstance "SQL01"
Show only differences for a specific AG
Compare-sqmAlwaysOnLogins -SqlInstance "SQL01" -AvailabilityGroupName "AG_Prod" -OnlyDifferences
Pipe to Format-Table for a quick console overview
Compare-sqmAlwaysOnLogins -SqlInstance "SQL01" | Format-Table
SQL Agent job step — fail the step when login drift is detected
Compare-sqmAlwaysOnLogins -SqlInstance "SQL01" -AvailabilityGroupName "AG_Prod" -FailOnDrift -OnlyDifferences