functions/Read-DbaTraceFile.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
function Read-DbaTraceFile {
    <#
  .SYNOPSIS
  Reads a trace file from specied SQL Server Database
 
  .DESCRIPTION
  Using the fn_trace_gettable function, a trace file is read and returned as a PowerShell object
 
  This function returns the whole of the trace file. The information is presented in the format that the trace subsystem uses.
 
  .PARAMETER SqlInstance
  A SQL Server instance to connect to
 
  .PARAMETER SqlCredential
  A credential to use to conect to the SQL instance rather than using Windows Authentication
 
  .PARAMETER Path
  Path to the trace file. This path is relative to the SQL Server instance.
    
  .PARAMETER Database
  Search for results only with specific DatabaseName. Uses IN for comparisons.
    
  .PARAMETER Login
  Search for results only with specific Logins. Uses IN for comparisons.
 
  .PARAMETER Spid
  Search for results only with specific Spids. Uses IN for comparisons.
 
  .PARAMETER EventClass
  Search for results only with specific EventClasses. Uses IN for comparisons.
 
  .PARAMETER ObjectType
  Search for results only with specific ObjectTypes. Uses IN for comparisons.
 
  .PARAMETER Error
  Search for results only with specific Errors. Uses IN for comparisons.
 
  .PARAMETER EventSequence
  Search for results only with specific EventSequences. Uses IN for comparisons.
 
  .PARAMETER TextData
  Search for results only with specific TextData. Uses LIKE for comparisons.
 
  .PARAMETER ApplicationName
  Search for results only with specific ApplicationNames. Uses LIKE for comparisons.
 
  .PARAMETER ObjectName
  Search for results only with specific ObjectNames. Uses LIKE for comparisons.
 
  .PARAMETER Where
  Custom where clause - use without the word "WHERE". Here are the available columns:
 
  TextData
  BinaryData
  DatabaseID
  TransactionID
  LineNumber
  NTUserName
  NTDomainName
  HostName
  ClientProcessID
  ApplicationName
  LoginName
  SPID
  Duration
  StartTime
  EndTime
  Reads
  Writes
  CPU
  Permissions
  Severity
  EventSubClass
  ObjectID
  Success
  IndexID
  IntegerData
  ServerName
  EventClass
  ObjectType
  NestLevel
  State
  Error
  Mode
  Handle
  ObjectName
  DatabaseName
  FileName
  OwnerName
  RoleName
  TargetUserName
  DBUserName
  LoginSid
  TargetLoginName
  TargetLoginSid
  ColumnPermissions
  LinkedServerName
  ProviderName
  MethodName
  RowCounts
  RequestID
  XactSequence
  EventSequence
  BigintData1
  BigintData2
  GUID
  IntegerData2
  ObjectID2
  Type
  OwnerID
  ParentName
  IsSystem
  Offset
  SourceDatabaseID
  SqlHandle
  SessionLoginName
  PlanHandle
  GroupID
    
  .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: Security, Trace
  Website: https://dbatools.io
  Copyright: (C) Chrissy LeMaire, clemaire@gmail.com
  License: GNU GPL v3 https://opensource.org/licenses/GPL-3.0
 
  .EXAMPLE
  Read-DbaTraceFile -SqlInstance sql2016 -Database master, tempdb -Path C:\traces\big.trc
 
  Reads the tracefile C:\traces\big.trc, stored on the sql2016 sql server. Filters only results that have master or tempdb as the DatabaseName.
    
  .EXAMPLE
  Read-DbaTraceFile -SqlInstance sql2016 -Database master, tempdb -Path C:\traces\big.trc -TextData 'EXEC SP_PROCOPTION'
 
  Reads the tracefile C:\traces\big.trc, stored on the sql2016 sql server.
  Filters only results that have master or tempdb as the DatabaseName and that have 'EXEC SP_PROCOPTION' somewhere in the text.
    
  .EXAMPLE
  Read-DbaTraceFile -SqlInstance sql2016 -Path C:\traces\big.trc -Where "LinkedServerName = 'myls' and StartTime > '5/30/2017 4:27:52 PM'"
 
  Reads the tracefile C:\traces\big.trc, stored on the sql2016 sql server.
  Filters only results where LinkServerName = myls and StartTime is greater than '5/30/2017 4:27:52 PM'.
 
#>

    [CmdletBinding()]
    Param (
        [parameter(Position = 0, Mandatory, ValueFromPipeline)]
        [Alias("ServerInstance", "SqlServer")]
        [DbaInstanceParameter[]]$SqlInstance,
        [PSCredential]$SqlCredential,
        [string[]]$Path,
        [string[]]$Database,
        [string[]]$Login,
        [int[]]$Spid,
        [string[]]$EventClass,
        [string[]]$ObjectType,
        [int[]]$Error,
        [int[]]$EventSequence,
        [string[]]$TextData,
        [string[]]$ApplicationName,
        [string[]]$ObjectName,
        [string]$Where,
        [switch][Alias('Silent')]$EnableException
    )
    
    process {
        
        if ($where) {
            $Where = "where $where"
        }
        elseif ($Database -or $Login -or $Spid) {
            
            $tempwhere = @()
            
            if ($Database) {
                $where = $database -join "','"
                $tempwhere += "databasename in ('$where')"
            }
            
            if ($Login) {
                $where = $Login -join "','"
                $tempwhere += "LoginName in ('$where')"
            }
            
            if ($Spid) {
                $where = $Spid -join ","
                $tempwhere += "Spid in ($where)"
            }
            
            if ($EventClass) {
                $where = $EventClass -join ","
                $tempwhere += "EventClass in ($where)"
            }
            
            if ($ObjectType) {
                $where = $ObjectType -join ","
                $tempwhere += "ObjectType in ($where)"
            }
            
            if ($Error) {
                $where = $Error -join ","
                $tempwhere += "Error in ($where)"
            }
            
            if ($EventSequence) {
                $where = $EventSequence -join ","
                $tempwhere += "EventSequence in ($where)"
            }
            
            if ($TextData) {
                $where = $TextData -join "%','%"
                $tempwhere += "TextData like ('%$where%')"
            }
            
            if ($ApplicationName) {
                $where = $ApplicationName -join "%','%"
                $tempwhere += "ApplicationName like ('%$where%')"
            }
            
            if ($ObjectName) {
                $where = $ObjectName -join "%','%"
                $tempwhere += "ObjectName like ('%$where%')"
            }
            
            $tempwhere = $tempwhere -join " and "
            $Where = "where $tempwhere"
        }
        
        foreach ($instance in $sqlInstance) {
            try {
                $server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $SqlCredential -MinimumVersion 9
            }
            catch {
                Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
                return
            }
            
            if (Test-Bound -Parameter Path) {
                $currentpath = $path
            }
            else {
                $currentpath = $server.ConnectionContext.ExecuteScalar("Select path from sys.traces where is_default = 1")
            }
            
            foreach ($file in $currentpath) {
                Write-Message -Level Verbose -Message "Parsing $file"
                
                $exists = Test-DbaSqlPath -SqlInstance $server -Path $file
                
                if (!$exists) {
                    Write-Message -Level Warning -Message "Path does not exist" -Target $file
                    Continue
                }
                
                $sql = "select SERVERPROPERTY('MachineName') AS ComputerName,
           ISNULL(SERVERPROPERTY('InstanceName'), 'MSSQLSERVER') AS InstanceName,
           SERVERPROPERTY('ServerName') AS SqlInstance,
         * FROM [fn_trace_gettable]('$file', DEFAULT) $Where"

                try {
                    $server.Query($sql)
                }
                catch {
                    Stop-Function -Message "Error returned from SQL Server: $_" -Target $server -InnerErrorRecord $_
                }
            }
        }
    }
}