Get-sqmLoginLastAccess
Security & Audit
sqmSQLTool v1.9.x · Get
Two sources, newest value wins:
Live (sys.dm_exec_sessions, only sessions that exist right now, wiped by every service restart, can never look further back than sqlserver_start_time) and ErrorLog (successful login messages in the SQL Server error log, only available when the AuditLevel registry value is 1 or 3, the default of 2 records nothing usable here, and limited by log rotation).LastAccess = $null means "not provable", NOT "never used". That distinction is deliberately not collapsed — a login can be actively used entirely outside both sources' coverage window.The error log parser is language-neutral: message templates for events 18453 / 18454 are read from
sys.messages and turned into regular expressions at runtime, since wording and even quoting differ per SQL Server language (English: user 'x', German: Benutzer "x").Parameters
| Parameter | Type | Required | Default | Notes |
|---|---|---|---|---|
| -SqlInstance | string[] | Optional | $env:COMPUTERNAME | One or more instances. Pipeline-capable. |
| -SqlCredential | PSCredential | Optional | , | SQL login credentials. |
| -Login | string[] | Optional | @() (all) | Filter: only these logins. Wildcards allowed. |
| -ExcludeSystemLogins | switch | Switch | $false | Hide NT SERVICE\*, NT AUTHORITY\* (incl. localized variants) and ##MS_*##. |
| -Source | string | Optional | All | Which sources to query: All, Live, ErrorLog. |
| -OutputPath | string | Optional | , | If given, CSV and HTML reports are written and copied to the central path. |
| -ContinueOnError | switch | Switch | $false | Continue with the next instance on error instead of aborting the whole run. |
| -EnableException | switch | Switch | $false | Throw exceptions immediately. |
| -NoOpen | switch | Switch | $false | Do not automatically open the generated report after creation. |
Execution Flow
Output object
| Field | Contents |
|---|---|
SqlInstance / LoginName / LoginType | Which login, on which instance, and its type (SQL_LOGIN, WINDOWS_LOGIN, WINDOWS_GROUP). |
IsDisabled / CreateDate | From sys.server_principals. |
LastAccess | Newest provable timestamp, or $null if nothing could be proven. |
DaysSince | Days between LastAccess and the server's own current time. $null if LastAccess is $null. |
Source | Live, ErrorLog, or None. |
Confidence | Exact (a source proved a value) or Unknown (nothing provable — not "never used"). |
ActiveSessions | Current session count for this login from the Live source, 0 if none. |
CoverageSince | Instance's sqlserver_start_time — the Live source cannot see further back than this. |
ErrorLogUsable | Was the ErrorLog source actually usable for this instance (AuditLevel 1/3 and a resolvable message template)? |
Note | Explains an empty result, or flags a Windows group login. |
Examples
Check one instance, all sources
Get-sqmLoginLastAccess -SqlInstance "SQL01"
Live sessions only, hide system logins
Get-sqmLoginLastAccess -SqlInstance "SQL01" -Source Live -ExcludeSystemLogins
Find logins with no provable access at all
Get-sqmLoginLastAccess -SqlInstance "SQL01" | Where-Object Confidence -eq 'Unknown'
Multiple instances via pipeline, CSV + HTML evidence
"SQL01", "SQL02" | Get-sqmLoginLastAccess -OutputPath "D:\Reports\LoginAudit" -NoOpen
Requirements
Requires dbatools. Needs VIEW SERVER STATE for the Live source; the ErrorLog source additionally needs securityadmin or sysadmin (xp_instance_regread / xp_readerrorlog). Windows GROUP logins usually show no session of their own — a member connects with their own AD account, so sys.dm_exec_sessions reports the account, not the group. Such logins carry an explanatory Note.
See also
Get-sqmLoginPermissions, what a login can actually do.
Get-sqmSysadminAccounts, who currently holds sysadmin.