Execution Flow
All operations are idempotent — granting an already-granted sysadmin role or revoking a non-existent one produce no error. On failure the calling Agent job is NOT deleted so the failure remains visible in job history. The PBM policy is always re-enabled in a
finally block even if login creation fails.Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| -SqlInstance | String | Optional | Target SQL Server instance. Default: $env:COMPUTERNAME. |
| -SqlCredential | PSCredential | Optional | SQL authentication credentials. |
| -Login | String | Required | Windows/AD login (e.g. DOMAIN\u.maier or an AD group). |
| -Action | String | Required | Grant or Revoke. |
| -CreateLoginIfMissing | Switch | Optional | Grant only: creates the AD login via CREATE LOGIN … FROM WINDOWS if not present. |
| -RemoveLogin | Switch | Optional | Revoke only: drops the login after revocation — only if it holds no other server roles (safety check). |
| -DisablePolicy | Bool | Optional | Disable the configured PBM policy before creating the login. Default: $true. |
| -TicketNumber | String | Optional | Ticket/order number included in all log and Event Log entries. |
| -JobName | String | Optional | Name of the calling Agent job. Deleted via sp_delete_job on success (self-deleting job pattern). |
Return Value
Returns PSCustomObject: SqlInstance, Login, Action, LoginCreated, LoginRemoved, TicketNumber, JobDeleted, Status, Message, Timestamp. On failure, throws so the calling Agent job records the error.
Examples
Example 1 — Manual early revocation
Invoke-sqmTempSysadminAction -SqlInstance SQL01 -Login 'DOM\u.maier' -Action Revoke
Example 2 — Revoke and remove login
Invoke-sqmTempSysadminAction -Login 'DOM\u.maier' -Action Revoke -RemoveLogin
Example 3 — Grant with auto-create login (from Agent job)
Invoke-sqmTempSysadminAction -SqlInstance SQL01 -Login 'DOM\u.maier' -Action Grant -CreateLoginIfMissing -TicketNumber "TKT-4711" -JobName "sqmGrant_DOM_u.maier"