· Uwe Janke
Commands / Register-sqmBlockedProcessMonitor

Register-sqmBlockedProcessMonitor

Blocking & LockingsqmSQLTool v1.9.101+ · Register🔓 ALTER ANY EVENT SESSION
The built-in system_health session does not reliably include blocked_process_report on every instance and version. Verified live: on a default installation the event is simply absent from system_health's definition, so once blocked process threshold (s) is greater than 0 the events fire but are captured nowhere at all. This function guarantees a capture target exists, independent of whatever system_health happens to contain on a given instance, which is why Get-sqmBlockingHistory calls it. Session name sqm_BlockedProcessMonitor, events sqlserver.blocked_process_report and sqlserver.xml_deadlock_report, target package0.ring_buffer (in-memory, about 4 MB, wraps around). With -IncludeFileTarget an additional package0.event_file target is added for longer retention (50 MB, 5 rollover files) under the instance's own error log directory, resolved via SERVERPROPERTY('ErrorLogFileName') so it is guaranteed writable by the service account. STARTUP_STATE=ON is set so the session survives an instance restart, and an existing session is never dropped or recreated, only started if needed, so a file target added once is preserved. This function does not change blocked process threshold (s) - that stays a deliberate, separate admin decision.

Parameters

ParameterTypeRequiredDefaultNotes
-SqlInstancestringOptional$env:COMPUTERNAMETarget SQL Server instance.
-SqlCredentialPSCredentialOptional, SQL or Windows credential. Without it: Windows authentication.
-IncludeFileTargetswitchSwitch$falseAlso add a package0.event_file target for retention beyond the ring buffer. Only takes effect when the session is first created.
-FileTargetPathstringOptionalinstance error log directoryDirectory for the event_file target. Only used together with -IncludeFileTarget.
-EnableExceptionswitchSwitch$falseThrow exceptions immediately instead of returning as errors.
-WhatIf / -ConfirmswitchSwitch$falseStandard ShouldProcess support: show or confirm the CREATE/ALTER EVENT SESSION before it runs.

Execution Flow

START dbatools installed? NO throw: dbatools not found YES sqm_BlockedProcessMonitor already exists? YES Never drop or recreate Only ensure STATE=START A file target added earlier is preserved NO CREATE EVENT SESSION sqm_BlockedProcessMonitor Events: sqlserver.blocked_process_report + sqlserver.xml_deadlock_report Target: package0.ring_buffer (in-memory, ~4 MB, wraps around) -IncludeFileTarget? NO Ring buffer only Retention limited to the in-memory window YES Add package0.event_file target 50 MB, 5 rollover files · -FileTargetPath or the instance error log directory Path resolved via SERVERPROPERTY('ErrorLogFileName') - writable by the service account ALTER EVENT SESSION ... STATE = START, STARTUP_STATE = ON The session survives an instance restart Return [PSCustomObject] SqlInstance · SessionName · Action · Message DONE

Examples

Create and start the monitor session
Register-sqmBlockedProcessMonitor -SqlInstance "SQL01"
With a file target for longer retention
Register-sqmBlockedProcessMonitor -SqlInstance "SQL01" -IncludeFileTarget
Dry run: show what would be created
Register-sqmBlockedProcessMonitor -SqlInstance "SQL01" -WhatIf
Set up capture end to end (the threshold stays a separate decision)
Register-sqmBlockedProcessMonitor -SqlInstance "SQL01" -IncludeFileTarget
# blocked process threshold is 0 by default - raise it deliberately, e.g. to 10 seconds:
Set-DbaSpConfigure -SqlInstance "SQL01" -Name "blocked process threshold (s)" -Value 10