Execution Flow
-FailOnDrift is built for unattended SQL Agent job use: a job step that only calls Compare-sqmAlwaysOnRoles -FailOnDrift writes the report as usual but then throws if any drift was found, turning a red job step into an OnFailure-operator alert. The report is always written first, even when the function subsequently throws. Database roles (db_owner, etc.) are deliberately out of scope — they live inside the replicated database itself and rarely diverge; orphaned SIDs there are handled by Repair-sqmAlwaysOnDatabases instead.Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| -SqlInstance | String | Optional | Entry-point instance of the AG (primary or a secondary). Default: local computer name. |
| -AvailabilityGroupName | String | Optional | AG to compare. Without it, the first AG found is used (warns if there's more than one). |
| -SqlCredential | PSCredential | Optional | PSCredential used for all replicas. |
| -IncludeSystemLogins | Switch | Optional | Also compare system logins (sa, ##MS_*, NT SERVICE\*, NT AUTHORITY\*, BUILTIN\*). Hidden by default. |
| -Login | String[] | Optional | Only compare these logins (wildcards allowed). |
| -ExcludeLogin | String[] | Optional | Exclude these logins (wildcards allowed). |
| -OnlyDifferences | Switch | Optional | Only output logins with a Warning or Critical status. |
| -OutputPath | String | Optional | Output directory for TXT/HTML. Default: from module configuration. |
| -NoOpen | Switch | Optional | Suppress automatically opening the HTML report. |
| -FailOnDrift | Switch | Optional | On drift (Warning/Critical), write a Windows Event (Source sqmSQLTool, EventId 9010) and throw — fails a calling SQL Agent job step. Implies -NoOpen; the report is still written first. |
| -ContinueOnError | Switch | Optional | Continue on error instead of aborting. |
| -EnableException | Switch | Optional | Throw errors immediately as exceptions. |
Return Value
Returns a PSCustomObject[] — one entry per compared login — with: LoginName, LoginType, OverallStatus (OK, Warning, or Critical), Present (e.g. "2/2"), MissingOn, RolesConsistent (bool), Roles, IsSysadminDrift (bool), Details (per-replica breakdown). Also writes TXT and HTML reports.
Examples
Example 1 — Compare roles for the first AG found
Compare-sqmAlwaysOnRoles -SqlInstance "SQL01"Example 2 — Show only logins with drift, for a named AG
Compare-sqmAlwaysOnRoles -SqlInstance "SQL01" -AvailabilityGroupName "AG_Prod" -OnlyDifferencesExample 3 — Unattended SQL Agent job step (fails the job on drift)
Compare-sqmAlwaysOnRoles -SqlInstance "SQL01" -FailOnDriftExample 4 — Pipe results into a table
Compare-sqmAlwaysOnRoles -SqlInstance "SQL01" | Format-Table