dbatools icon indicating copy to clipboard operation
dbatools copied to clipboard

Read-DbaXEFile - Test-Path RemoteTargetFile False Without Wildcard

Open mattcargile opened this issue 4 years ago • 9 comments

Verified issue does not already exist?

Yes

What error did you receive?

PSMessageDetails      : 
Exception             : System.Exception: 
\\ServerName\C$\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\Log\XeSessionName 
                        cannot be accessed from LocalServerName. Does domain\user have access?
TargetObject          : 
CategoryInfo          : NotSpecified: (:) [], Exception
FullyQualifiedErrorId : dbatools_Read-DbaXEFile
ErrorDetails          : 
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at Stop-Function, 
                 C:\Program Files\WindowsPowerShell\Modules\dbatools\1.1.11\allcommands.ps1: line 91469
                        at Read-DbaXEFile<Process>, 
                 C:\Program Files\WindowsPowerShell\Modules\dbatools\1.1.11\allcommands.ps1: line 59945
                        at Select-DefaultView<Process>, 
                 C:\Program Files\WindowsPowerShell\Modules\dbatools\1.1.11\allcommands.ps1: line 88935
                        at Get-DbaXESession<Process>, 
                 C:\Program Files\WindowsPowerShell\Modules\dbatools\1.1.11\allcommands.ps1: line 39354
                        at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {}

Steps to Reproduce

Get-DbaXESession $SqlInstance -Session $XeSessionName -EnableException | Read-DbaXEFile -EnableException

Are you running the latest release?

Yes

Other details or mentions

I think the issue was introduced in https://github.com/sqlcollaborative/dbatools/commit/a69925a68f03e26918ff7b3628f832b481cb090c# with the removal of lines 101 to 109.

Now the below line would return $false on all (Get-DbaXESession).RemoteTargetFiles.

https://github.com/sqlcollaborative/dbatools/blob/fb2f08f69524af3f85fb0b6617a3b1d3419a41cd/functions/Read-DbaXEFile.ps1#L90

Considering the aforementioned commit, I think the the other Read* cmdlets may be impacted.

What PowerShell host was used when producing this error

PowerShell Core (pwsh.exe), Windows PowerShell (powershell.exe), VS Code (integrated terminal)

PowerShell Host Version


Name                           Value
----                           -----
PSVersion                      7.1.4
PSEdition                      Core
GitCommitId                    7.1.4
OS                             Microsoft Windows 10.0.18363
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
Name                           Value     
----                           -----                               
PSVersion                      5.1.18362.1171         
PSEdition                      Desktop                
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.18362.1171        
CLRVersion                     4.0.30319.42000        
WSManStackVersion              3.0                    
PSRemotingProtocolVersion      2.3                    
SerializationVersion           1.1.0.1                

SQL Server Edition and Build number

Microsoft SQL Server 2017 (RTM-CU24) (KB5001228) - 14.0.3391.2 (X64) 
Apr 28 2021 10:32:18 
Copyright (C) 2017 Microsoft Corporation
Enterprise Edition: Core-based Licensing (64-bit) 
on Windows Server 2016 Standard 10.0 <X64> (Build 14393: ) (Hypervisor)

mattcargile avatar Aug 23 '21 14:08 mattcargile

I agree. The lines were removed with #7603. @potatoqualitee, do you remember why you removed the lines?

Here the needed code before current line 90 for reference:

            if (-not $Exact) {
                $currentfile = $currentfile.Replace('.xel', '*.xel')
                $currentfile = $currentfile.Replace('.xem', '*.xem')

                if ($currentfile -notmatch "xel" -and $currentfile -notmatch "xem") {
                    $currentfile = "$currentfile*.xel"
                }
            }

andreasjordan avatar Aug 31 '21 15:08 andreasjordan

yeah, the new library doesnt do wildcards 😭

potatoqualitee avatar Sep 01 '21 08:09 potatoqualitee

Oh no! Can this pattern be used instead?

Read-SQLXEvent -ConnectionString "Server=.;Database=master;Trusted_Connection=True;" -SessionName perf_session

This is Example 2 on the Microsoft Docs.

mattcargile avatar Sep 01 '21 16:09 mattcargile

Been thinking more about it. So with Example 3 in the Comment Help:

https://github.com/sqlcollaborative/dbatools/blob/fb2f08f69524af3f85fb0b6617a3b1d3419a41cd/functions/Read-DbaXEFile.ps1#L41-L42

One would need to pipe results from Get-DbaXESession into Get-ChildItem or the like to grab the array of files? Then you could pipe that into the Read*

mattcargile avatar Sep 07 '21 18:09 mattcargile

that seems like a solid solution, @mattcargile. ill check it out

potatoqualitee avatar Sep 13 '21 08:09 potatoqualitee

Been thinking more about it. So with Example 3 in the Comment Help:

https://github.com/sqlcollaborative/dbatools/blob/fb2f08f69524af3f85fb0b6617a3b1d3419a41cd/functions/Read-DbaXEFile.ps1#L41-L42

One would need to pipe results from Get-DbaXESession into Get-ChildItem or the like to grab the array of files? Then you could pipe that into the Read*

I did try this solution with pwsh and it only works on the XE file that isn't currently being written to. Here is the ErrorRecord.

MessageDetails      : 
Exception             : System.IO.IOException: The process cannot access the file 
                        '\\SERVERNAME\e$\MSSQL11.SERVERNAME\MSSQL\Log\SessionName_0_132769660623030000.xel' 
                          because it is being used by another process.
                           at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)
                           at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, 
                           FileOptions options)
                           at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, 
                           FileShare share, Int32 bufferSize, FileOptions options)
                           at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, 
                           FileShare share, Int32 bufferSize, Boolean useAsync)
                           at Microsoft.SqlServer.XEvent.XELite.XEFileEventStreamer..ctor(String fileName)
                           at XEModuleCore.SqlXEventCmdlet.OutputXELFile(String fileName)
                           at XEModuleCore.SqlXEventCmdlet.ProcessRecord()
                           at System.Management.Automation.CommandProcessor.ProcessRecord()
TargetObject          : 
CategoryInfo          : NotSpecified: (:) [Read-SqlXEvent], IOException
FullyQualifiedErrorId : System.IO.IOException,XEModuleCore.SqlXEventCmdlet
ErrorDetails          : 
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at Read-DbaXEFile<Process>, 
                             C:\Program Files\WindowsPowerShell\Modules\dbatools\1.1.20\allcommands.ps1: line 59937
                        at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {}

Oh no! Can this pattern be used instead?

Read-SQLXEvent -ConnectionString "Server=.;Database=master;Trusted_Connection=True;" -SessionName perf_session

This is Example 2 on the Microsoft Docs.

When I attempt this solution, it appears I can only view the live feed as the data is written to the file and to my pwsh session. Is the only solution to read the actively written file to wait for it to rollover or stop the session?

ADDENDUM: I guess it may be because of the removal of the Microsoft.SqlServer.XEvent.Linq.QueryableXEventData class discussed in the issue below? This is what was giving us the live data? @potatoqualitee , did you ever speak with Microsoft on the timeline for them to bring it back in future versions of SMO. I'm assuming a native code to managed code conversion of the aforementioned class would be years down to the road to never and the solution is to have a two XE sessions that you can flip on and off as you read and process XE events? https://github.com/microsoft/sqltoolsservice/issues/807

mattcargile avatar Sep 24 '21 14:09 mattcargile

Are there any new ideas about this?

andreasjordan avatar Dec 21 '21 09:12 andreasjordan

I'm having a hard time concentrating enough to solve the issue, as it's pretty complex :/

potatoqualitee avatar Dec 21 '21 09:12 potatoqualitee

I haven't thought about the issue since my last posts. It appears one can't read the active,live xel file on the newer builds of SMO or at least the build we have in dbatools. Not sure if we can include multiple builds so we can use the Queryable Events library. Otherwise I just pipe the files directly to the Read cmdlet. Maybe there could be a feature to stop the session and then read the files and then turn it back on.

mattcargile avatar Dec 21 '21 13:12 mattcargile

Let me bring this one up again. Can we do anything about this? Or should we close?

andreasjordan avatar Oct 25 '22 08:10 andreasjordan

I'll revisit in 2.0

potatoqualitee avatar Oct 26 '22 07:10 potatoqualitee

This got a bit stale. So let me bring this issue to you attention again...

andreasjordan avatar Jun 10 '23 14:06 andreasjordan

As we currently depend on SqlServer.XEvents, we have a problem:

image https://www.powershellgallery.com/packages/SqlServer.XEvent

andreasjordan avatar Jun 21 '23 12:06 andreasjordan

Moved a lot of code around and now it is working again (on my maschine). But it needs SqlServer module installed. Feedback welcome.

andreasjordan avatar Jun 21 '23 13:06 andreasjordan

Uhhh no idea why I didn't implement this? Read-XEvent now exists in dbatools.dll and can be used in lieu of the sql server command.

potatoqualitee avatar Jun 22 '23 12:06 potatoqualitee

Thanks for the feedback, updates the pull request.

andreasjordan avatar Jun 22 '23 12:06 andreasjordan