Skip to content

Commit

Permalink
Watch-DbaDbLogin - Only write to target and don't watch target (datap…
Browse files Browse the repository at this point in the history
…lat#8936)

Co-authored-by: Chrissy LeMaire <potatoqualitee@users.noreply.github.com>
  • Loading branch information
andreasjordan and potatoqualitee authored Jun 7, 2023
1 parent c8ca1ab commit 617a162
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions public/Watch-DbaDbLogin.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function Watch-DbaDbLogin {
param (
[DbaInstance]$SqlInstance,
[PSCredential]$SqlCredential,
[object]$Database,
[string]$Database,
[string]$Table = "DbaTools-WatchDbLogins",
# Central Management Server
[string]$SqlCms,
Expand All @@ -95,6 +95,18 @@ function Watch-DbaDbLogin {
)

process {
if (Test-Bound 'SqlCms', 'ServersFromFile', 'InputObject' -Not) {
Stop-Function -Message "You must specify a server list source using -SqlCms or -ServersFromFile or pipe in connected instances. See the command documentation and examples for more details."
return
}

try {
$serverDest = Connect-DbaInstance -SqlInstance $SqlInstance -SqlCredential $SqlCredential
} catch {
Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $SqlInstance
return
}

$systemdbs = "master", "msdb", "model", "tempdb"
$excludedPrograms = "Microsoft SQL Server Management Studio - Query", "SQL Management"

Expand All @@ -118,14 +130,6 @@ function Watch-DbaDbLogin {
}
}

if ($SqlInstance) {
if ($servers) {
$servers += $SqlInstance
} else {
$servers = $SqlInstance
}
}

<#
Connect each server
#>
Expand Down Expand Up @@ -188,7 +192,7 @@ function Watch-DbaDbLogin {
$procs = $procs | Where-Object { $systemdbs -notcontains $_.Database -and $excludedPrograms -notcontains $_.Program }

if ($procs.Count -gt 0) {
$procs | Select-Object @{Label = "ComputerName"; Expression = { $instance.ComputerName } }, @{Label = "InstanceName"; Expression = { $instance.ServiceName } }, @{Label = "SqlInstance"; Expression = { $instance.DomainInstanceName } }, LoginTime, Login, Host, Program, DatabaseId, Database, IsSystem, CaptureTime | ConvertTo-DbaDataTable | Write-DbaDbTableData -SqlInstance $instance -Database $Database -Table $Table -AutoCreateTable
$procs | Select-Object @{Label = "ComputerName"; Expression = { $instance.ComputerName } }, @{Label = "InstanceName"; Expression = { $instance.ServiceName } }, @{Label = "SqlInstance"; Expression = { $instance.DomainInstanceName } }, LoginTime, Login, Host, Program, DatabaseId, Database, IsSystem, CaptureTime | ConvertTo-DbaDataTable | Write-DbaDbTableData -SqlInstance $serverDest -Database $Database -Table $Table -AutoCreateTable

Write-Message -Level Output -Message "Added process information for $instance to datatable."
} else {
Expand Down

0 comments on commit 617a162

Please sign in to comment.