Install-sqmStandardJob
Always OnsqmSQLTool v1.9.168+ · New🔓 SQLAgentOperatorRole / sysadmin on the job instance
New-sqmAgentCommandJob (generic CmdExec wrapper with typed .clixml parameters). -List shows the catalog.| Name | Runs | Defaults |
|---|---|---|
| AgLoginSync | Sync-sqmLoginsToAlwaysOn | -SyncMode Union -Force:$false, daily 02:00 and after every failover (Agent alert on message 1480), job sqmAutoLoginSync |
| AgLoginCompare | Compare-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
| Parameter | Type | Required | Default | Notes |
|---|---|---|---|---|
| -Name | string | Required | , | Catalog entry: AgLoginSync or AgLoginCompare. |
| -List | switch | Switch | $false | Show the catalog with all defaults instead of installing anything. |
| -SqlInstance | string | Optional | $env:COMPUTERNAME | Instance where the job is created. Run the function on that machine, the wrapper files are written locally. |
| -SqlCredential | PSCredential | Optional | , | Only for the connection that creates the job. The step runs as the SQL Agent service account, which needs sysadmin on all replicas. |
| -AvailabilityGroupName | string | Optional | first AG | AG for the AG jobs. Also narrows the PrimaryOnly check to that AG. |
| -JobName | string | Optional | from catalog | Override the job name. |
| -Parameters | hashtable | Optional | , | Merged over the catalog parameters, your values win. Example: @{ AuditAdOrphans = $true }. |
| -ScheduleType | string | Optional | from catalog | None, Daily, Weekly or Monthly. |
| -ScheduleTime | string HH:mm | Optional | from catalog | Start time of the schedule. |
| -ScheduleDays | string[] | Optional | , | Weekdays for -ScheduleType Weekly. |
| -Force | switch | Switch | $false | Replace an existing job of the same name. |
| -StartJob | switch | Switch | $false | Run the job once immediately after creating it. |
| -NoFailoverAlert | switch | Switch | $false | AgLoginSync only: do not create the failover alert on message 1480. Removes an alert of the same name left over from an earlier installation. |
| -EnableException | switch | Switch | $false | Throw exceptions immediately instead of returning a result object with Status Failed. |
| -WhatIf / -Confirm | switch | Switch | ConfirmImpact = Medium | Show or confirm the installation before anything is written. |
Execution Flow
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'