# dbatools

#### 0.9.791

 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 #ValidationTags#Messaging,FlowControl,Pipeline,CodeStyle# 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. Windows and SQL Authentication supported. Accepts credential objects (Get-Credential)       .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(Position = 0, Mandatory, ValueFromPipelineByPropertyName)] [Alias("ServerInstance", "SqlServer")] [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, [Alias('Silent')] [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 occured while establishing connection to $instance" -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-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"                 try {                     $server.Query($sql)                 } catch {                     Stop-Function -Message "Error returned from SQL Server: $_" -Target$server -InnerErrorRecord \$_                 }             }         }     } }