functions/New-DbaXESmartCsvWriter.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
function New-DbaXESmartCsvWriter {
    <#
        .SYNOPSIS
            This Response type is used to write Extended Events to a CSV file.
 
        .DESCRIPTION
            This Response type is used to write Extended Events to a CSV file.
 
        .PARAMETER OutputFile
            Specifies the path to the output CSV file.
 
        .PARAMETER Overwrite
            Specifies whether any existiting file should be overwritten or not.
 
        .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 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), netnerds.net
            Website: https://dbatools.io
            Copyright: (C) Chrissy LeMaire, clemaire@gmail.com
            License: MIT https://opensource.org/licenses/MIT
            SmartTarget: by Gianluca Sartori (@spaghettidba)
 
        .LINK
            https://dbatools.io/New-DbaXESmartCsvWriter
            https://github.com/spaghettidba/XESmartTarget/wiki
 
        .EXAMPLE
            $columns = "cpu_time", "duration", "physical_reads", "logical_reads", "writes", "row_count"
            $response = New-DbaXESmartCsvWriter -OutputFile c:\temp\workload.csv -OutputColumn $columns -OverWrite -Event "sql_batch_completed"
            Start-DbaXESmartTarget -SqlInstance localhost\sql2017 -Session "Profiler Standard" -Responder $response
 
            Writes Extended Events to the file "C:\temp\workload.csv".
    #>

    [CmdletBinding()]
    param (
        [parameter(Mandatory)]
        [string]$OutputFile,
        [switch]$Overwrite,
        [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 }

        try {
            $writer = New-Object -TypeName XESmartTarget.Core.Responses.CsvAppenderResponse
            $writer.OutputFile = $OutputFile
            $writer.OverWrite = $Overwrite
            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
        }
    }
}