Sql-server – Extended Events XEL file reader; Is there a FileName property avaialble in QueryTableXEventData or SqlBulkCopy

extended-eventssql server

Using the QueryTableXEventData class in Powershell I am able to parse 100's of XEL files very quickly and export their content to a table in SQL Server using SQLBulkCopy

Example:

$events = new-object Microsoft.SqlServer.XEvent.Linq.QueryableXEventData
("\\some_file_path\XELog_Files*.xel")

I don't see though a File property in the the $fields array.

$event | Select-Object -ExpandProperty Fields

is_cached     System.Boolean              True
is_recovered  System.Boolean             False
is_dac        System.Boolean             False
database_id   System.UInt32                 73
packet_size   System.UInt32               8000
options       System.Byte[]  {32, 0, 0, 40...}
options_text  System.String
database_name System.String

Or in $events

Name      : login
UUID      : 13e22e12-3cb8-49bf-a3e1-131faa95601c
Package   : Microsoft.SqlServer.XEvent.Linq.Internal.XEventInteropPackage
Metadata  : Microsoft.SqlServer.XEvent.Linq.Internal.XEventInteropEventMetadata
Timestamp : 18/08/2020 03:41:37 +00:00
Fields    : {is_cached, is_recovered, is_dac, database_id...}
Actions   : {server_instance_name, database_name, database_id, client_hostname...}
Location  : Microsoft.SqlServer.XEvent.Linq.EventLocator

Is there a property I can use in SQLBulkCopy or QueryTableXEventData that has the file name currently being read or exported?

Best Answer

I don't believe the file name is exposed via a property.

Instead of passing a wildcard path to the QueryableXEventData constructor, you could get a list of the files and process each individually:

$eventFiles = Get-Item "\\some_file_path\XELog_Files*.xel"
foreach($eventFile in $eventFiles) {
    $events = new-object Microsoft.SqlServer.XEvent.Linq.QueryableXEventData($eventFile.FullName)
    foreach($event in $events) {
        # event processing here, using $eventFile.FullName or $eventFile.Name
    }
}