Grant-sqmTemporarySysadmin
Security
sqmSQLTool v1.9.59+ · Grant
✓ -WhatIf supported · ✓ Windows/AD logins and existing SQL-Auth logins
sysadmin or dbcreator rights (-Role, default sysadmin) for N days via self-deleting SQL Agent jobs. For patching, installation, and incident-response situations. Both Windows/AD logins (auto-created if missing, via CREATE LOGIN ... FROM WINDOWS) and already-existing SQL-Auth logins are supported; a missing SQL-Auth login raises a clear error instead of being auto-created. 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. No interactive confirmation prompt by default (-TicketNumber already documents the deliberate decision) — pass -Confirm explicitly to re-enable it.
ⓘ 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.
ⓘ While creating/altering/removing the login, all currently-enabled server-wide DDL triggers and the configured PBM policy are temporarily disabled and restored to their exact prior state — otherwise a Policy-Based Management "On Change: Prevent" policy or a custom DDL trigger on
ALTER SERVER ROLE/CREATE LOGIN can silently roll the grant back ("The transaction ended in the trigger."). See Invoke-sqmTempSysadminAction.
Parameters
| Parameter | Type | Required | Default | Notes |
|---|---|---|---|---|
| -SqlInstance | string | Optional | $env:COMPUTERNAME | Default instance of the local computer. |
| -Login | string | Required | , | DOMAIN\Account, an AD group, or an already-existing SQL-Auth login. A missing SQL-Auth login raises an error instead of being auto-created. |
| -Role | string | Optional | sysadmin | sysadmin or dbcreator. |
| -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' `
-Forcedbcreator for an existing SQL-Auth login
Grant-sqmTemporarySysadmin `
-SqlInstance SQL01 `
-Login 'app_deploy' `
-Role dbcreator `
-Days 1 `
-TicketNumber 'CHG9001'