forked from dataplat/dbatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExport-DbaXECsv.ps1
147 lines (122 loc) · 5.34 KB
/
Export-DbaXECsv.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
function Export-DbaXECsv {
<#
.SYNOPSIS
Exports Extended Events to a CSV file.
.DESCRIPTION
Exports Extended Events to a CSV file.
.PARAMETER Path
Specifies the directory where the file or files will be exported.
.PARAMETER FilePath
Specifies the full file path of the output file.
.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.
.PARAMETER InputObject
Allows Piping
.NOTES
Tags: ExtendedEvent, XE, XEvent
Author: Gianluca Sartori (@spaghettidba)
Website: https://dbatools.io
Copyright: (c) 2018 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
.LINK
https://dbatools.io/Export-DbaXECsv
.EXAMPLE
PS C:\> Get-ChildItem -Path C:\temp\sample.xel | Export-DbaXECsv -Path c:\temp\sample.csv
Writes Extended Events data to the file "C:\temp\events.csv".
.EXAMPLE
PS C:\> Get-DbaXESession -SqlInstance sql2014 -Session deadlocks | Export-DbaXECsv -Path c:\temp\events.csv
Writes Extended Events data to the file "C:\temp\events.csv".
#>
[CmdletBinding()]
param (
[parameter(Mandatory, ValueFromPipeline)]
[Alias('FullName')]
[object[]]$InputObject,
[string]$Path = (Get-DbatoolsConfigValue -FullName 'Path.DbatoolsExport'),
[Alias("OutFile", "FileName")]
[string]$FilePath,
[switch]$EnableException
)
begin {
$null = Test-ExportDirectory -Path $Path
try {
$xedll = Join-DbaPath -Path $script:libraryroot -ChildPath lib, third-party, XESmartTarget, XESmartTarget.Core.dll
Add-Type -Path $xedll -ErrorAction Stop
} catch {
Stop-Function -Message "Could not load XESmartTarget.Core.dll" -ErrorRecord $_ -Target "XESmartTarget"
return
}
function Get-FileFromXE ($InputObject) {
if ($InputObject.TargetFile) {
if ($InputObject.TargetFile.Length -eq 0) {
Stop-Function -Message "This session does not have an associated Target File."
return
}
$instance = [dbainstance]$InputObject.ComputerName
if ($instance.IsLocalHost) {
$xelpath = $InputObject.TargetFile
} else {
$xelpath = $InputObject.RemoteTargetFile
}
#this is funny, TargetFile usually is more a path than a real file
#Even if the filename property is set to a fixed filepath with a xel
#extension, the produced files have something appended to it to make
#sure max_file_size and max_rollover_files are respected.
#so, even if file ends with .xel, we need to pick up <filename>*.xel
if (-not($xelpath.EndsWith(".xel"))) {
$xelpath = "$xelpath*.xel"
} else {
$xelpath = "$($xelpath.Substring(0, $xelpath.Length-4))*.xel"
}
try {
Get-ChildItem -Path $xelpath -ErrorAction Stop
} catch {
Stop-Function -Message "Failure" -ErrorRecord $_
}
}
}
}
process {
if (Test-FunctionInterrupt) { return }
$getfiles = Get-FileFromXE $InputObject
if ($getfiles) {
$InputObject += $getfiles
}
foreach ($file in $InputObject) {
if ($file -is [System.String]) {
$currentfile = $file
} elseif ($file -is [System.IO.FileInfo]) {
$currentfile = $file.FullName
} elseif ($file -is [Microsoft.SqlServer.Management.XEvent.Session]) {
# it was taken care of above
continue
} else {
Stop-Function -Message "Unsupported file type."
return
}
$accessible = Test-Path -Path $currentfile
$whoami = whoami
if (-not $accessible) {
if ($file.Status -eq "Stopped") { continue }
Stop-Function -Continue -Message "$currentfile cannot be accessed from $($env:COMPUTERNAME). Does $whoami have access?"
}
$FilePath = Get-ExportFilePath -Path $PSBoundParameters.Path -FilePath $PSBoundParameters.FilePath -Type csv -ServerName $instance
$adapter = New-Object XESmartTarget.Core.Utils.XELFileCSVAdapter
$adapter.InputFile = $currentfile
$adapter.OutputFile = $FilePath
try {
$adapter.Convert()
$file = Get-ChildItem -Path $adapter.OutputFile
if ($file.Length -eq 0) {
Remove-Item -Path $adapter.OutputFile
} else {
$file
}
} catch {
Stop-Function -Message "Failure" -ErrorRecord $_ -Target "XESmartTarget" -Continue
}
}
}
}