/
Read-DbaTraceFile.ps1
280 lines (230 loc) 路 9.05 KB
/
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
274
275
276
277
278
279
280
function Read-DbaTraceFile {
<#
.SYNOPSIS
Reads SQL Server trace files
.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
The target SQL Server instance or instances.
.PARAMETER 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.
.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 ErrorId
Search for results only with specific Errors. Filters 'Error in ($ErrorId)' 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
Author: Chrissy LeMaire (@cl), netnerds.net
Website: https://dbatools.io
Copyright: (c) 2018 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
.EXAMPLE
PS C:\> 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
PS C:\> 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
PS C:\> 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'.
.EXAMPLE
PS C:\> Get-DbaTrace -SqlInstance sql2014 | Read-DbaTraceFile
Reads every trace file on sql2014
#>
[CmdletBinding()]
param (
[parameter(Mandatory, ValueFromPipelineByPropertyName)]
[DbaInstanceParameter[]]$SqlInstance,
[parameter(ValueFromPipelineByPropertyName)]
[PSCredential]$SqlCredential,
[parameter(ValueFromPipelineByPropertyName)]
[string[]]$Path,
[string[]]$Database,
[string[]]$Login,
[int[]]$Spid,
[string[]]$EventClass,
[string[]]$ObjectType,
[int[]]$ErrorId,
[int[]]$EventSequence,
[string[]]$TextData,
[string[]]$ApplicationName,
[string[]]$ObjectName,
[string]$Where,
[switch]$EnableException
)
begin {
if ($where) {
$Where = "where $where"
} elseif ($Database -or $Login -or $Spid -or $ApplicationName -or $EventClass -or $ObjectName -or $ObjectType -or $EventSequence -or $ErrorId) {
$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 ($ErrorId) {
$where = $ErrorId -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"
}
}
process {
foreach ($instance in $sqlInstance) {
try {
$server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $SqlCredential -MinimumVersion 9
} catch {
Stop-Function -Message "Error occurred while establishing connection to $instance" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
}
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-DbaPath -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"
Write-Message -Message "SQL: $sql" -Level Debug
try {
$server.Query($sql)
} catch {
Stop-Function -Message "Error returned from SQL Server: $instance" -Target $server -InnerErrorRecord $_
}
}
}
}
}