functions/New-DbaXESmartTableWriter.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#ValidationTags#Messaging,FlowControl,Pipeline,CodeStyle#
function New-DbaXESmartTableWriter {
    <#
    .SYNOPSIS
        This response type is used to write Extended Events to a database table.
 
    .DESCRIPTION
        This response type is used to write Extended Events to a database table. The events are temporarily stored in memory before being written to the database at regular intervals.
 
        The target table can be created manually upfront or you can let the TableAppenderResponse create a target table based on the fields and actions available in the events captured.
 
        The columns of the target table and the fields/actions of the events are mapped by name (case-sensitive).
 
    .PARAMETER SqlInstance
        The target SQL Server instance or instances. You must have sysadmin access and server version must be SQL Server version 2008 or higher.
 
    .PARAMETER SqlCredential
        Login to the target instance using alternative credentials. Windows and SQL Authentication supported. Accepts credential objects (Get-Credential)
 
    .PARAMETER Database
        Specifies the name of the database that contains the target table.
 
    .PARAMETER Table
        Specifies the name of the target table.
 
    .PARAMETER AutoCreateTargetTable
        If this switch is enabled, XESmartTarget will infer the definition of the target table from the columns captured in the Extended Events session.
 
        If the target table already exists, it will not be recreated.
 
    .PARAMETER UploadIntervalSeconds
        Specifies the number of seconds XESmartTarget will keep the events in memory before dumping them to the target table. The default is 10 seconds.
 
    .PARAMETER OutputColumn
        Specifies the list of columns to output from the events. XESmartTarget will capture in memory and write to the target table only the columns (fields or targets) that are present in this list.
 
        Fields and actions are matched in a case-sensitive manner.
 
        Expression columns are supported. Specify a column with ColumnName AS Expression to add an expression column (Example: Total AS Reads + Writes)
 
    .PARAMETER Event
        Specifies a list of events to be processed (with others being ignored. By default, all events are processed.
 
    .PARAMETER Filter
        Specifies a filter expression in the same form as you would use in the WHERE clause of a SQL query.
 
        Example: duration > 10000 AND cpu_time > 10000
 
    .PARAMETER WhatIf
        If this switch is enabled, no actions are performed but informational messages will be displayed that explain what would happen if the command were to run.
 
    .PARAMETER Confirm
        If this switch is enabled, you will be prompted for confirmation before executing any operations that change state.
 
    .PARAMETER 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.
 
    .NOTES
        Tags: ExtendedEvent, XE, XEvent
        Author: Chrissy LeMaire (@cl) | SmartTarget by Gianluca Sartori (@spaghettidba)
 
        Website: https://dbatools.io
        Copyright: (c) 2018 by dbatools, licensed under MIT
        License: MIT https://opensource.org/licenses/MIT
        SmartTarget: by Gianluca Sartori (@spaghettidba)
 
    .LINK
        https://dbatools.io/New-DbaXESmartTableWriter
 
    .EXAMPLE
        PS C:\> $columns = "cpu_time", "duration", "physical_reads", "logical_reads", "writes", "row_count", "batch_text"
        PS C:\> $response = New-DbaXESmartTableWriter -SqlInstance sql2017 -Database dbadb -Table deadlocktracker -OutputColumn $columns -Filter "duration > 10000"
        PS C:\> Start-DbaXESmartTarget -SqlInstance sql2017 -Session deadlock_tracker -Responder $response
 
        Writes Extended Events to the deadlocktracker table in dbadb on sql2017.
 
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param (
        [parameter(Mandatory, ValueFromPipeline)]
        [Alias("ServerInstance", "SqlServer")]
        [DbaInstanceParameter[]]$SqlInstance,
        [PSCredential]$SqlCredential,
        [parameter(Mandatory)]
        [string]$Database,
        [parameter(Mandatory)]
        [string]$Table,
        [switch]$AutoCreateTargetTable,
        [int]$UploadIntervalSeconds = 10,
        [string[]]$Event,
        [string[]]$OutputColumn,
        [string]$Filter,
        [switch]$EnableException
    )
    begin {
        try {
            Add-Type -Path "$script:PSModuleRoot\bin\XESmartTarget\XESmartTarget.Core.dll" -ErrorAction Stop
        } catch {
            Stop-Function -Message "Could not load XESmartTarget.Core.dll" -ErrorRecord $_ -Target "XESmartTarget"
            return
        }
    }
    process {
        if (Test-FunctionInterrupt) { return }

        foreach ($instance in $SqlInstance) {
            try {
                $server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $SqlCredential -MinimumVersion 11
            } catch {
                Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
            }
            if ($Pscmdlet.ShouldProcess($instance, "Creating new XESmartTableWriter")) {
                try {
                    $writer = New-Object -TypeName XESmartTarget.Core.Responses.TableAppenderResponse
                    $writer.ServerName = $server.Name
                    $writer.DatabaseName = $Database
                    $writer.TableName = $Table
                    $writer.AutoCreateTargetTable = $AutoCreateTargetTable
                    $writer.UploadIntervalSeconds = $UploadIntervalSeconds
                    if (Test-Bound -ParameterName "Event") {
                        $writer.Events = $Event
                    }
                    if (Test-Bound -ParameterName "OutputColumn") {
                        $writer.OutputColumns = $OutputColumn
                    }
                    if (Test-Bound -ParameterName "Filter") {
                        $writer.Filter = $Filter
                    }
                    $writer
                } catch {
                    Stop-Function -Message "Failure" -ErrorRecord $_ -Target "XESmartTarget" -Continue
                }
            }
        }
    }
}