Execution Flow
-SourceCredential and -DestinationCredential are separate credentials for primary and secondary connections, useful in mixed-auth environments. -BackupLogins exports logins to a .sql file before syncing (audit trail). -AuditAdOrphans 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 -SourceCredential). |
| -SourceCredential | PSCredential | Optional | Explicit credential for the source/primary instance. |
| -DestinationCredential | PSCredential | Optional | Credential for secondary replica connections. |
| -AvailabilityGroupName | 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. |
| -AuditAdOrphans | 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. |
| -AdjustAuthMode | switch | Optional | When set, automatically adjust target replica authentication mode to match primary if needed. |
| -BackupPath | string | Optional | Path where login backups are stored. Default: configured output path (Get-sqmDefaultOutputPath), i.e. C:\System\WinSrvLog\MSSQL unless overridden in the module config. Path is created if it doesn't exist. |
| -BackupRetentionDays | int | Optional | When greater than 0, login backups (LoginBackup_*.sql) in BackupPath older than this many days are deleted after the sync. With -AuditAdOrphans, the LoginAudit_<instance>_* reports are cleaned up too. Default: 7. Set to 0 to disable cleanup (keep all files). |
| -DisablePolicy | switch | Optional | Disable SQL Server policies on secondaries during the copy (default: $true). |
| -Force | switch | Optional | Existing logins on secondaries are overwritten (password / language / default-db drift), not only new ones added. Default: $true - so a bare 'Sync-sqmLoginsToAlwaysOn' keeps the secondaries fully in sync. Opt out with -Force:$false (then only new logins are created). With SafeForceMode=true (default), all sysadmin logins, the SQL Agent account and system logins (sa via SID, NT SERVICE\*, etc.) are automatically excluded - no self-lockout. |
| -ForceExclude | string[] | Optional | Additional logins to exclude from Force operation (blacklist). Combined with SafeForceMode exclusions. Default: none. |
| -ForceIncludeOnly | string[] | Optional | When Force is set with this parameter, only these logins are updated (whitelist). Overrides other login filters. System logins still excluded per SafeForceMode. Example: 'AppUser_*', 'ServiceAccount' |
| -IncludeSystemLogins | switch | Optional | When set, system logins are also copied. Default: $false. |
| -NoReport | switch | Optional | See Sync-sqmLoginsToAlwaysOn -? for details. |
| -RestartServiceIfRequired | switch | Optional | When set, restart the SQL Server service on secondary replicas if auth mode was changed. |
| -SafeForceMode | bool | Optional | When Force is set and SafeForceMode is true (default), automatically excludes dangerous logins: - sa (system admin) - SQL Agent Service Account - NT SERVICE\* (virtual accounts) - BUILTIN\* (Windows built-in accounts) Set to false ONLY if you fully understand the risks. Default: $true |
| -SkipSecondaryServers | string[] | Optional | Comma-separated list of secondary instance names to skip (for maintenance). Example: 'SQL02', 'SQL03' |
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" -AvailabilityGroupName "AG_PROD"Example 2, Sync with backup and orphan audit
Sync-sqmLoginsToAlwaysOn -SqlInstance "SQL01" -BackupLogins -AuditAdOrphansExample 3, Sync specific logins, exclude sa
Sync-sqmLoginsToAlwaysOn -SqlInstance "SQL01" -Login "DOMAIN\AppUser","svc_app" -ExcludeLogin "sa"