Execution Flow
Synopsis
Queries session and connection DMVs to give an instant view of who is connected to SQL Server and from where. Requires VIEW SERVER STATE. The -GroupBy parameter controls how connections are aggregated. Active requests (non-sleeping sessions) are always included as a separate list. Connection usage percentage is calculated from sys.configurations max connections.
Uses the
_s localization helper for all output strings. Requires dbatools and VIEW SERVER STATE permission on the target instance.Syntax
Get-sqmConnectionStats [-SqlInstance <String[]>] # pipeline [-SqlCredential <PSCredential>] [-GroupBy <String>] # Application|Login|Host|Database [-OutputPath <String>] [-OutputHtml] [-ContinueOnError] [-EnableException]
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| -SqlInstance | String[] | $env:COMPUTERNAME | SQL Server instance(s) to query. Pipeline-capable. |
| -SqlCredential | PSCredential | — | Credential for the SQL connection. |
| -GroupBy | String | Application | Grouping dimension: Application (program_name), Login (login_name), Host (host_name), or Database (database_name). |
| -OutputPath | String | — | If set: write CSV to this directory. |
| -OutputHtml | Switch | false | Generate an HTML dashboard via ConvertTo-sqmHtmlReport. |
| -ContinueOnError | Switch | false | Continue with the next instance on error. |
| -EnableException | Switch | false | Throw terminating exceptions. |
Return Value
| Property | Description |
|---|---|
| SqlInstance | The queried SQL Server instance. |
| Summary | PSCustomObject: TotalConnections, MaxConnections, ConnectionUsagePct, ActiveRequests, ActiveSessions. |
| GroupedStats | Array sorted by ConnectionCount DESC: GroupKey, ConnectionCount, LoginNames[], HostNames[]. |
| ActiveRequests | Sessions with non-sleeping status: SessionId, LoginName, HostName, Database, Status, WaitType, BlockedBy. |
| CsvFile | Path to CSV file (if -OutputPath was set). |
| HtmlFile | Path to HTML file (if -OutputHtml was set). |
Examples
# Connection summary grouped by application Get-sqmConnectionStats -SqlInstance "SQL01" # Group by login and save CSV Get-sqmConnectionStats -SqlInstance "SQL01" -GroupBy "Login" -OutputPath "C:\System\WinSrvLog\MSSQL" # Show active requests only (Get-sqmConnectionStats).ActiveRequests | Format-Table SessionId, LoginName, Status, WaitType