Execution Flow
-SourceSqlCredential and -DestinationSqlCredential are separate credentials for primary and secondary connections — useful in mixed-auth environments. -BackupLogins exports logins to a .sql file before syncing (audit trail). -AuditOrphans reports database users on the secondary that have no matching server login after the sync.Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| -SqlInstance | String | Optional | Primary replica instance. Default: $env:COMPUTERNAME. |
| -SqlCredential | PSCredential | Optional | SQL credential for the primary (alias for -SourceSqlCredential). |
| -SourceSqlCredential | PSCredential | Optional | Explicit credential for the source/primary instance. |
| -DestinationSqlCredential | PSCredential | Optional | Credential for secondary replica connections. |
| -AvailabilityGroup | String | Optional | Target AG name. If omitted, all AGs on the instance are processed. |
| -Login | String[] | Optional | Sync only these specific login names. |
| -ExcludeLogin | String[] | Optional | Login names to skip during sync. |
| -BackupLogins | Switch | Optional | Export logins to a .sql backup file before syncing. |
| -AuditOrphans | Switch | Optional | After sync, report orphaned database users on secondaries. |
| -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 login per secondary — with: Primary, Secondary, Login, Status, Message.
Examples
Example 1 — Sync all logins to all AG secondaries
Sync-sqmLoginsToAlwaysOn -SqlInstance "SQL01" -AvailabilityGroup "AG_PROD"
Example 2 — Sync with backup and orphan audit
Sync-sqmLoginsToAlwaysOn -SqlInstance "SQL01" -BackupLogins -AuditOrphans
Example 3 — Sync specific logins, exclude sa
Sync-sqmLoginsToAlwaysOn -SqlInstance "SQL01" -Login "DOMAIN\AppUser","svc_app" -ExcludeLogin "sa"