· Uwe Janke
Commands / Register-sqmAuditSession

Register-sqmAuditSession

SecuritysqmSQLTool v1.9.115+ · Register🔓 ALTER ANY EVENT SESSION
Creates (if missing) and starts a dedicated Extended Events session covering the login/database/metadata "audit problem": -FailedLogins (error_reported filtered to 18456), -SuccessfulLogins (login), -DatabaseCreated/-DatabaseDropped (database_created/database_dropped, always instance-wide), and -MetadataChanges (object_created/object_altered/object_deleted, filtered to the commit phase, optionally scoped to specific databases with -TargetDatabase). Every category is independently selectable, or use -All. No dedicated reader is needed: every event collects the same standard actions Invoke-sqmExtendedEvents -Read already surfaces.

Parameters

ParameterTypeRequiredDefaultNotes
-SqlInstancestringOptional$env:COMPUTERNAMETarget SQL Server instance.
-SqlCredentialPSCredentialOptional, SQL or Windows credential.
-SessionNamestringOptionalsqm_AuditSessionName of the Extended Events session.
-FailedLoginsswitchSwitch$falseCapture failed login attempts (error 18456).
-SuccessfulLoginsswitchSwitch$falseCapture successful logins.
-DatabaseCreatedswitchSwitch$falseCapture CREATE DATABASE. Always instance-wide, see below.
-DatabaseDroppedswitchSwitch$falseCapture DROP DATABASE. Always instance-wide, see below.
-MetadataChangesswitchSwitch$falseCapture schema-level DDL (object created/altered/deleted).
-AllswitchSwitch$falseShorthand for every category above.
-TargetDatabasestring[]Optional, Scopes -MetadataChanges to these databases. Has no effect on -DatabaseCreated/-DatabaseDropped (verified live: no working predicate exists for those two events).
-IncludeSystemGeneratedObjectsswitchSwitch$falseAlso capture temp objects (#...) and auto-generated statistics (_WA_Sys_...) under -MetadataChanges. Excluded by default as noise.
-IncludeFileTargetswitchSwitch$falseAdds a durable package0.event_file target alongside the ring buffer. Only applied when the session is first created.
-FileTargetPathstringOptionalinstance error log dirDirectory for the event_file target, only used with -IncludeFileTarget.
-EnableExceptionswitchSwitch$falseThrow exceptions immediately instead of returning as errors.

Execution Flow

START dbatools installed? NO throw: dbatools not found YES Any category or -All chosen? NO throw: choose ≥1 category YES If -TargetDatabase set without -MetadataChanges Log WARNING: only -MetadataChanges can be scoped by database name Session already exists? NO Build ADD EVENT clauses per selected switch error_reported(error_number=18456) · login · database_created · database_dropped object_created/altered/deleted WHERE ddl_phase=1 AND NOT #temp/_WA_Sys_ AND [db scope] -IncludeFileTarget? Resolve ErrorLog dir via SERVERPROPERTY, add package0.event_file ShouldProcess → CREATE EVENT SESSION ring_buffer(4MB) + optional event_file · STARTUP_STATE=ON ALTER EVENT SESSION ... STATE = START Action = Created YES Switches do NOT alter an existing session's definition (log INFO) Currently running? YES Action = Unchanged NO ShouldProcess → ALTER ... STATE = START Action = Started Return [PSCustomObject] SqlInstance · SessionName · Action · Categories[] · Message DONE

Verified Live, Not Assumed

Examples

Login auditing only
Register-sqmAuditSession -SqlInstance "SQL01" -FailedLogins -SuccessfulLogins
Every category, with durable file-based retention
Register-sqmAuditSession -SqlInstance "SQL01" -All -IncludeFileTarget
Schema-change auditing scoped to two databases, nothing else
Register-sqmAuditSession -SqlInstance "SQL01" -MetadataChanges -TargetDatabase "Orders", "Billing"
Set up, then read back with the module's existing Extended Events reader
Register-sqmAuditSession -SqlInstance "SQL01" -All
Invoke-sqmExtendedEvents -SqlInstance "SQL01" -SessionName sqm_AuditSession -Read