· Uwe Janke
Commands / New-sqmAlwaysOnLoginSyncJob
Always On

New-sqmAlwaysOnLoginSyncJob

Creates the SQL Agent job for the automatic AlwaysOn login sync. Since v1.9.168.0 this is a shortcut for Install-sqmStandardJob -Name AgLoginSync: Sync-sqmLoginsToAlwaysOn -SyncMode Union -Force:$false daily at 2:00 AM and, via an Agent alert on message 1480, right after every failover. Logins missing on any replica are added in every direction and password changes are applied to all replicas, nothing is dropped. Install it on every replica, only the current primary does the work.

Module: sqmSQLTool
Requires: dbatools
ShouldProcess: Yes
Output: PSCustomObject

Execution Flow

START Collect -AvailabilityGroupName, -AuditAdOrphans, -BackupRetentionDays Install-sqmStandardJob -Name AgLoginSync (job sqmAutoLoginSync) CmdExec step, PrimaryOnly, -SyncMode Union, daily 02:00 Agent alert on message 1480 starts it after a failover DONE
On a secondary the job step ends with success without doing anything, so after a failover the new primary takes over. Jobs created by older versions (step calls jobs\Sync-Job.ps1) keep running; recreate them with -Force to switch over. Jobs created since v1.9.168.0 use the Union sync automatically after the module update (it is the new default of Sync-sqmLoginsToAlwaysOn); recreate them with -Force once to also get the failover alert.
Renamed in v1.9.209.0 from New-sqmAutoLoginSyncJob; the old name still works as an alias. The parameter file is named <Job>_<Instance>_<Step>.clixml, so several instances on one server do not overwrite each other. Logs and reports default to <SQL Server installation drive>:\sqm\Log and \sqm\Output, where the Agent service account can write. Only one Agent alert on message 1480 exists per instance: with two AGs on one instance the second job is not started by the alert.

Parameters

ParameterTypeRequiredDescription
-SqlInstanceStringOptionalTarget SQL instance. Default: $env:COMPUTERNAME. Can be a remote server: the wrapper and parameter file are then written to that server via the C$ admin share (the module must be installed there).
-AvailabilityGroupNameStringOptionalAG to sync. Default: the first AG on the instance.
-JobNameStringOptionalName of the Agent job. Default: sqmAutoLoginSync. A second AG on the same instance needs its own name.
-AuditAdOrphansSwitchOptionalAfter each run, report Windows logins without an AD account (event 9003). Detection only, nothing is deleted.
-BackupRetentionDaysIntOptionalRetention for login backups and audit reports in days. Sync default: 7, 0 keeps all.
-NoFailoverAlertSwitchOptionalDo not create the Agent alert on message 1480 that starts the job after a failover.
-ForceSwitchOptionalOverwrites an existing job with the same name.
-WhatIfSwitchOptionalPreview actions without creating the job.
-ConfirmSwitchOptionalPrompt before creating the job.

Return Value

Returns the result of Install-sqmStandardJob: Name, SqlInstance, JobName, FunctionName, Parameters, ScheduleName, PrimaryOnly, AlertName, AlertStatus, Started, Status, Message, Timestamp.

Examples

Example 1, Create job with default schedule

New-sqmAlwaysOnLoginSyncJob -SqlInstance "SQL01"

Example 2, Replace an old job, with AD orphan report

New-sqmAlwaysOnLoginSyncJob -SqlInstance "SQL01" -AvailabilityGroupName "ProdAG" -AuditAdOrphans -Force