commands

^

Watch-DbaDbLogin

Author Chrissy LeMaire (@cl), netnerds.net
Availability Windows, Linux, macOS

 

Synopsis

Tracks SQL Server logins: which host they came from, what database they're using, and what program is being used to log in.

Description

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.

Syntax

Watch-DbaDbLogin
    [-SqlInstance] <DbaInstanceParameter>
    [[-Database] <Object>]
    [[-Table] <String>]
    [[-SqlCredential] <PSCredential>]
    [[-SqlCms] <String>]
    [[-ServersFromFile] <String>]
    [[-InputObject] <Server[]>]
    [-EnableException]
    [<CommonParameters>]

 

Examples

 

Example: 1
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.

Example: 2
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.

Example: 3
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.

Example: 4
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.

Required Parameters

-SqlInstance

The SQL Server that stores the Watch database.

Alias
Required True
Pipeline false
Default Value

Optional Parameters

-Database

The name of the Watch database.

Alias
Required False
Pipeline false
Default Value
-Table

The name of the Watch table. By default, this is DbaTools-WatchDbLogins.

Alias
Required False
Pipeline false
Default Value DbaTools-WatchDbLogins
-SqlCredential

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
-SqlCms

Specifies a Central Management Server to query for a list of servers to watch.

Alias
Required False
Pipeline false
Default Value
-ServersFromFile

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
-InputObject

Allows pipeline input from Connect-DbaInstance.

Alias
Required False
Pipeline true (ByValue)
Default Value
-EnableException

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

 

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.