· Uwe Janke
Commands / Install-sqmStandardJob

Install-sqmStandardJob

Always OnsqmSQLTool v1.9.168+ · New🔓 SQLAgentOperatorRole / sysadmin on the job instance
Recurring tasks that every AlwaysOn installation needs are kept in a small catalog: target function, safe parameter defaults, job name and schedule. One call turns a catalog entry into a SQL Server Agent job, no parameter hashtables needed. The job is built by New-sqmAgentCommandJob (generic CmdExec wrapper with typed .clixml parameters). -List shows the catalog.

NameRunsDefaults
AgLoginSyncSync-sqmLoginsToAlwaysOn-SyncMode Union -Force:$false, daily 02:00 and after every failover (Agent alert on message 1480), job sqmAutoLoginSync
AgLoginCompareCompare-sqmAlwaysOnLogins-FailOnDrift -OnlyDifferences, daily 06:00, job sqmAutoLoginCompare

Install the AG jobs on every replica. They are PrimaryOnly: the job step first checks the local AG role and, on a secondary, ends with success without calling the function. After a failover the new primary simply takes over and no job turns red.

Failover alert (v1.9.191.0+). AgLoginSync also creates the Agent alert <JobName> - AG failover (1480). Message 1480 is logged when a database changes its AG role, so the sync runs right after a failover instead of at the next 02:00. Because 1480 is raised once per database, the alert waits 600 seconds between responses. It fires on the old and the new primary; the PrimaryOnly check lets only the new primary work. SQL Agent allows one alert per condition: an existing 1480 alert without a job gets the sync job attached (its notifications stay), one that already starts another job is left alone and reported. -NoFailoverAlert skips it.

Union instead of primary to secondary. Logins are not part of the AG. A login created on the old primary shortly before a failover exists only on the new secondary, and a primary-to-secondary sync never finds it. The job therefore runs -SyncMode Union: every login missing on a replica is created from a replica that has it, and a password changed on one replica is applied to the others (since v1.9.192.0). Logins dropped on a replica (tombstones) and other conflicts are reported (event 9004), not changed. See Sync-sqmLoginsToAlwaysOn.

Why the login sync runs without -Force. -Force replaces existing logins by DROP and CREATE. If the CREATE fails afterwards (policy, transient error, AD latency) the login is gone on the secondary, which happened in unattended runs. The standard job therefore only adds missing logins and warns if you force Force = $true through -Parameters. Password changes are applied by the sync without DROP (ALTER LOGIN ... HASHED); default database or language drift is reported by the sync (event 9004) and by AgLoginCompare, and fixed deliberately by hand.

Parameters

ParameterTypeRequiredDefaultNotes
-NamestringRequired, Catalog entry: AgLoginSync or AgLoginCompare.
-ListswitchSwitch$falseShow the catalog with all defaults instead of installing anything.
-SqlInstancestringOptional$env:COMPUTERNAMEInstance where the job is created. Run the function on that machine, the wrapper files are written locally.
-SqlCredentialPSCredentialOptional, Only for the connection that creates the job. The step runs as the SQL Agent service account, which needs sysadmin on all replicas.
-AvailabilityGroupNamestringOptionalfirst AGAG for the AG jobs. Also narrows the PrimaryOnly check to that AG.
-JobNamestringOptionalfrom catalogOverride the job name.
-ParametershashtableOptional, Merged over the catalog parameters, your values win. Example: @{ AuditAdOrphans = $true }.
-ScheduleTypestringOptionalfrom catalogNone, Daily, Weekly or Monthly.
-ScheduleTimestring HH:mmOptionalfrom catalogStart time of the schedule.
-ScheduleDaysstring[]Optional, Weekdays for -ScheduleType Weekly.
-ForceswitchSwitch$falseReplace an existing job of the same name.
-StartJobswitchSwitch$falseRun the job once immediately after creating it.
-NoFailoverAlertswitchSwitch$falseAgLoginSync only: do not create the failover alert on message 1480. Removes an alert of the same name left over from an earlier installation.
-EnableExceptionswitchSwitch$falseThrow exceptions immediately instead of returning a result object with Status Failed.
-WhatIf / -ConfirmswitchSwitchConfirmImpact = MediumShow or confirm the installation before anything is written.

Execution Flow

START -List? YES Return the catalog Nothing is created NO Build the step parameters Catalog defaults, then SqlInstance and AvailabilityGroupName, then -Parameters (wins) AgLoginSync with Force = $true: warning about DROP + CREATE New-sqmAgentCommandJob CmdExec step: generic wrapper + typed .clixml file, flagged -PrimaryOnly Schedule from the catalog or your override, -Force replaces, -StartJob runs once AgLoginSync: failover alert on message 1480 Starts the job after a role change, 600 s between responses At every job run The wrapper asks sys.dm_hadr_availability_replica_states for the local role first Local replica primary? NO Step ends with success Function is not called YES Run the catalog function Sync-sqmLoginsToAlwaysOn or Compare-sqmAlwaysOnLogins with the stored parameters DONE

Return Value

PSCustomObject with Name, SqlInstance, JobName, FunctionName, Parameters, ScheduleName, PrimaryOnly, AlertName, AlertStatus (Created, Linked, Occupied, Removed, Failed or empty), Started, Status (Success or Failed), Message, Timestamp. With -List: one object per catalog entry.

Examples

Show the catalog with all defaults
Install-sqmStandardJob -List
Login sync on every replica of the AG (run on each node)
Install-sqmStandardJob -Name AgLoginSync -AvailabilityGroupName 'AG_Prod'
Also report Windows logins without an AD account (detection only)
Install-sqmStandardJob -Name AgLoginSync -AvailabilityGroupName 'AG_Prod' -Parameters @{ AuditAdOrphans = $true } -Force
Drift alarm: the job fails when logins differ between replicas
Install-sqmStandardJob -Name AgLoginCompare -AvailabilityGroupName 'AG_Prod' -ScheduleTime '07:30'