Grant-sqmTemporarySysadmin
Security
sqmSQLTool v1.8.2+ · Grant
✓ -WhatIf supported · ⚠ Windows/AD logins only
sysadmin rights for N days via self-deleting SQL Agent jobs. For patching, installation, and incident-response situations. Without -StartDate: rights are granted immediately, Revoke-Job is scheduled at now + N days. With -StartDate: both Grant-Job and Revoke-Job are scheduled. AlwaysOn-aware by default — creates independent jobs on every replica so rights survive a failover and cleanup runs reliably everywhere.
ⓘ You can run this against any instance in an AG — the function queries
sys.availability_replicas and creates its own local SQL Agent jobs on every replica automatically. To limit to the specified instance only, use -PrimaryOnly. Use -SkipSecondaryServers to exclude individual replicas by name.
Parameters
| Parameter | Type | Required | Default | Notes |
|---|---|---|---|---|
| -SqlInstance | string | Optional | $env:COMPUTERNAME | Default instance of the local computer. |
| -Login | string | Required | — | Must be DOMAIN\Account format. SQL auth logins are rejected immediately. |
| -Days | int | Required | — | Duration in days (1–3650). Revoke runs at StartDate + Days (or now + Days). |
| -StartDate | datetime | Optional | — (immediate) | Future activation date. If missing or in the past: immediate grant. |
| -PrimaryOnly | switch | Switch | $false | Skip AG replica detection, only treat specified instance. |
| -SkipSecondaryServers | string[] | Optional | @() | Replica names to exclude from job creation. |
| -TicketNumber | string | Optional | — | Written to module log and Windows Event Log for traceability. |
| -Force | switch | Switch | $false | Overwrite existing Grant/Revoke jobs with identical names. |
| -SqlCredential | PSCredential | Optional | — | Only used for immediate inline operations. Agent jobs run under the SQL Agent service account. |
Execution Flow
Examples
Grant immediately for 3 days — all AG replicas
Grant-sqmTemporarySysadmin `
-SqlInstance SQL01 `
-Login 'CORP\u.maier' `
-Days 3 `
-TicketNumber 'INC0012345'Scheduled — grant on 1 July 08:00, revoke 2 July 08:00
Grant-sqmTemporarySysadmin `
-Login 'CORP\u.maier' `
-Days 1 `
-StartDate '2026-07-01 08:00' `
-TicketNumber 'CHG7788'Preview only — primary instance, no replica jobs
Grant-sqmTemporarySysadmin `
-SqlInstance SQL01 `
-Login 'CORP\u.maier' `
-Days 2 `
-PrimaryOnly `
-WhatIfAll replicas except one specific secondary
Grant-sqmTemporarySysadmin `
-SqlInstance SQL01 `
-Login 'CORP\u.maier' `
-Days 5 `
-SkipSecondaryServers 'SQL03' `
-Force