· Uwe Janke
Commands / Get-sqmWhoIsActive

Get-sqmWhoIsActive

Session ManagementsqmSQLTool v1.9.127+ · Get🔓 VIEW SERVER STATE
Shows currently active and blocked sessions, in the spirit of Adam Machanic's sp_whoisactive but without installing anything on the target instance: everything is built from sys.dm_exec_sessions, sys.dm_exec_requests, sys.dm_exec_sql_text and sys.dm_db_session_space_usage, all available on any SQL Server 2012+ instance. One row per relevant session: SPID, login/host/program, database, status, blocking SPID, wait info, elapsed time, CPU/reads/writes, tempdb allocation and the running (or last) SQL statement. With -RepeatIntervalSeconds the query re-runs on an interval, like sp_whoisactive's @sleep_time but driven from the client side, until -RepeatCount iterations or -DurationMinutes elapse, or you press Ctrl+C. Every iteration is collected, and one CSV (full detail) plus one HTML report (last snapshot) is written at the end, including when the loop is cancelled early. For a live-refreshing grid instead of console output, use Show-sqmWhoIsActiveMonitor.

Parameters

ParameterTypeRequiredDefaultNotes
-SqlInstancestringOptional$env:COMPUTERNAMETarget SQL Server instance.
-SqlCredentialPSCredentialOptional, SQL or Windows credential. Without it: Windows authentication.
-ShowSleepingSpidsint (0,1,2)Optional1Mirrors sp_whoisactive's @show_sleeping_spids: 0 = only sessions with an active request, 1 = plus idle sessions with an open transaction, 2 = all user sessions.
-MinElapsedSecondsintOptional0Only report sessions whose current request (or last batch) has been running/idle for at least this many seconds.
-RepeatIntervalSecondsintOptional0Seconds between snapshots. 0 = run once, no repeat.
-RepeatCountintOptional0Number of snapshots when repeating. 0 = until -DurationMinutes elapses or Ctrl+C. Ignored when the interval is 0.
-DurationMinutesintOptional0Stop repeating after this many minutes. 0 = unlimited.
-OutputPathstringOptionalGet-sqmDefaultOutputPath\WhoIsActiveDirectory for the CSV/HTML report.
-NoConsoleOutputswitchSwitch$falseSuppress the live table printed after every snapshot. For unattended/Agent job runs where only the report matters.
-NoOpenswitchSwitch$falseDo not automatically open the generated report.
-EnableExceptionswitchSwitch$falseThrow exceptions immediately instead of returning as errors.

Execution Flow

START dbatools installed? NO throw: dbatools not found YES Query the session DMVs sys.dm_exec_sessions + dm_exec_requests + dm_exec_sql_text + dm_db_session_space_usage Statement-level SQL text via statement_start/end_offset, last batch for idle sessions Filter: -ShowSleepingSpids, -MinElapsedSeconds 0 = active requests only | 1 = plus idle-in-transaction | 2 = all user sessions Print snapshot as console table Skipped with -NoConsoleOutput · every snapshot is appended to AllSnapshots -RepeatIntervalSeconds > 0 and limit not reached? NO Leave the loop Also on Ctrl+C - the report is still written YES Sleep, then take the next snapshot Until -RepeatCount iterations or -DurationMinutes elapse Write CSV (all iterations) + HTML (last snapshot) Written in a finally block, so an interrupted run still produces its report Return [PSCustomObject] SqlInstance · StartTime · EndTime · Iterations · SnapshotCount LastSnapshot[] · AllSnapshots[] · CsvFile · HtmlFile DONE

Examples

Single snapshot, like running sp_whoisactive once
Get-sqmWhoIsActive -SqlInstance "SQL01"
Live monitor: refresh every 5 seconds until Ctrl+C
Get-sqmWhoIsActive -SqlInstance "SQL01" -RepeatIntervalSeconds 5
30 snapshots (5 minutes), only sessions busy for 5+ seconds
Get-sqmWhoIsActive -SqlInstance "SQL01" -RepeatIntervalSeconds 10 -RepeatCount 30 -MinElapsedSeconds 5
Unattended capture for an Agent job: one hour, no console output
Get-sqmWhoIsActive -SqlInstance "SQL01" -RepeatIntervalSeconds 30 -DurationMinutes 60 -NoConsoleOutput
Only the blocked sessions of the current snapshot
$w = Get-sqmWhoIsActive -SqlInstance "SQL01"
$w.LastSnapshot | Where-Object { $_.BlockingSpid -gt 0 }