Author | Chrissy LeMaire (@cl), netnerds.net |
Availability | Windows, Linux, macOS |
Want to see the source code for this command? Check out Watch-DbaDbLogin on GitHub.
Want to see the Bill Of Health for this command? Check out Watch-DbaDbLogin.
Tracks SQL Server logins: which host they came from, what database they're using, and what program is being used to log in.
Watch-DbaDbLogin uses SQL Server DMV's to track logins into a SQL Server table. This is helpful when you need to migrate a SQL Server and update connection strings, but have inadequate documentation on which servers/applications are logging into your SQL instance.
Running this script every 5 minutes for a week should give you a sufficient idea about database and login usage.
Logins from the same server are excluded from the watched set.
The inputs for this command are either a Central Management Server (registered server repository), a flat file of server names, or to use pipeline input via Connect-DbaInstance. See the examples for more information.
Watch-DbaDbLogin
[[-SqlInstance] <DbaInstanceParameter>]
[[-SqlCredential] <PSCredential>]
[[-Database] <String>]
[[-Table] <String>]
[[-SqlCms] <String>]
[[-ServersFromFile] <String>]
[[-InputObject] <Server[]>]
[-EnableException]
[<CommonParameters>]
PS C:\> Watch-DbaDbLogin -SqlInstance sqlserver -SqlCms SqlCms1
A list of all database instances within the Central Management Server SqlCms1 is generated. Using this list, the script enumerates all the processes and gathers login information and saves it to the
table Dblogins in the DatabaseLogins database on SQL Server sqlserver.
PS C:\> Watch-DbaDbLogin -SqlInstance sqlcluster -Database CentralAudit -ServersFromFile .\sqlservers.txt
A list of servers is gathered from the file sqlservers.txt in the current directory. Using this list, the script enumerates all the processes and gathers login information and saves it to the table
Dblogins in the CentralAudit database on SQL Server sqlcluster.
PS C:\> Watch-DbaDbLogin -SqlInstance sqlserver -SqlCms SqlCms1 -SqlCredential $cred
A list of servers is generated using database instance names within the SQL2014Clusters group on the Central Management Server SqlCms1. Using this list, the script enumerates all the processes and
gathers login information and saves it to the table Dblogins in the DatabaseLogins database on sqlserver.
PS C:\> $instance1 = Connect-DbaInstance -SqlInstance sqldev01
PS C:\> $instance2 = Connect-DbaInstance -SqlInstance sqldev02
PS C:\> $instance1, $instance2 | Watch-DbaDbLogin -SqlInstance sqltest01 -Database CentralAudit
Pre-connects two instances sqldev01 and sqldev02 and then using pipelining sends them to Watch-DbaDbLogin to enumerate processes and gather login info. The resulting gathered info is stored to the
DbaTools-WatchDbLogins table in the CentralAudit database on the sqltest01 instance.
Note: This is the method to use if the instances have different credentials than the instance used to store the watch data.
The SQL Server that stores the Watch database.
Alias | |
Required | False |
Pipeline | false |
Default Value |
Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.
For MFA support, please use Connect-DbaInstance.
Alias | |
Required | False |
Pipeline | false |
Default Value |
The name of the Watch database.
Alias | |
Required | False |
Pipeline | false |
Default Value |
The name of the Watch table. By default, this is DbaTools-WatchDbLogins.
Alias | |
Required | False |
Pipeline | false |
Default Value | DbaTools-WatchDbLogins |
Specifies a Central Management Server to query for a list of servers to watch.
Alias | |
Required | False |
Pipeline | false |
Default Value |
Specifies a file containing a list of servers to watch. This file must contain one server name per line.
Alias | |
Required | False |
Pipeline | false |
Default Value |
Allows pipeline input from Connect-DbaInstance.
Alias | |
Required | False |
Pipeline | true (ByValue) |
Default Value |
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
Alias | |
Required | False |
Pipeline | false |
Default Value | False |