Skip to content

Commit 071e793

Browse files
Read-DbaAuditFile / Read-DbaXEFile / Watch-DbaXESession - Refactoring and bugfixing (#9720)
1 parent 80c8fdd commit 071e793

7 files changed

+32
-35
lines changed

public/Read-DbaAuditFile.ps1

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,23 @@ function Read-DbaAuditFile {
8282
}
8383
}
8484

85-
$accessible = Test-Path -Path $currentFile
86-
$whoami = whoami
85+
# $currentFile is only the base filename and must be expanded using a wildcard
86+
$fileNames = (Get-ChildItem -Path ($currentFile -replace '\.sqlaudit$', '*.sqlaudit') | Sort-Object CreationTime).FullName
87+
$enum = @( )
88+
foreach ($fileName in $fileNames) {
89+
$accessible = Test-Path -Path $fileName
90+
$whoami = whoami
91+
if (-not $accessible) {
92+
Stop-Function -Continue -Message "$fileName cannot be accessed from $($env:COMPUTERNAME). Does $whoami have access?"
93+
}
8794

88-
if (-not $accessible) {
89-
if ($file.Status -eq "Stopped") { continue }
90-
Stop-Function -Continue -Message "$currentFile cannot be accessed from $($env:COMPUTERNAME). Does $whoami have access?"
95+
$enum += Read-XEvent -FileName $fileName
9196
}
9297

9398
if ($Raw) {
94-
return (SqlServer.XEvent\Read-SqlXEvent -FileName $currentfile)
99+
return $enum
95100
}
96101

97-
# use the SqlServer.XEvent\Read-SqlXEvent cmdlet from Microsoft
98-
# because the underlying Class uses Tasks
99-
# which is hard to handle in PowerShell
100-
101-
$enum = SqlServer.XEvent\Read-SqlXEvent -FileName $currentfile
102102
$newcolumns = ($enum.Fields.Name | Select-Object -Unique)
103103

104104
$actions = ($enum.Actions.Name | Select-Object -Unique)

public/Read-DbaXEFile.ps1

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,6 @@ function Read-DbaXEFile {
9494
Stop-Function -Message "$file cannot be accessed from $($env:COMPUTERNAME)." -Continue
9595
}
9696

97-
# use the SqlServer\Read-SqlXEvent cmdlet from Microsoft
98-
# because the underlying Class uses Tasks
99-
# which is hard to handle in PowerShell
100-
10197
if ($Raw) {
10298
try {
10399
Read-XEvent -FileName $file

public/Watch-DbaXESession.ps1

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function Watch-DbaXESession {
8080
$sessionname = $xesession.Name
8181
Write-Message -Level Verbose -Message "Watching $sessionname on $($server.Name)."
8282

83-
if (-not $xesession.IsRunning -and -not $xesession.IsRunning) {
83+
if (-not $xesession.IsRunning) {
8484
Stop-Function -Message "$($xesession.Name) is not running on $($server.Name)" -Continue
8585
}
8686

@@ -103,13 +103,10 @@ function Watch-DbaXESession {
103103

104104
try {
105105
if ($raw) {
106-
return (SqlServer.XEvent\Read-SqlXEvent -ConnectionString $server.ConnectionContext.ConnectionString -SessionName $sessionname -ErrorAction Stop)
106+
return (Read-XEvent -ConnectionString $server.ConnectionContext.ConnectionString -SessionName $sessionname -ErrorAction Stop)
107107
}
108108

109-
# use the SqlServer.XEvent\Read-SqlXEvent cmdlet from Microsoft
110-
# because the underlying Class uses Tasks
111-
# which is hard to handle in PowerShell
112-
SqlServer.XEvent\Read-SqlXEvent -ConnectionString $server.ConnectionContext.ConnectionString -SessionName $sessionname -ErrorAction Stop | ForEach-Object -Process {
109+
Read-XEvent -ConnectionString $server.ConnectionContext.ConnectionString -SessionName $sessionname -ErrorAction Stop | ForEach-Object -Process {
113110

114111
$hash = [ordered]@{ }
115112

tests/Read-DbaAuditFile.Tests.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
4343
}
4444
Context "Verifying command output" {
4545
It "returns some results" {
46-
$results = Get-DbaInstanceAudit -SqlInstance $TestConfig.instance2 -Audit LoginAudit | Read-DbaAuditFile -Raw -WarningAction SilentlyContinue
47-
[System.Linq.Enumerable]::Count($results) -gt 1 | Should Be $true
46+
$results = Get-DbaInstanceAudit -SqlInstance $TestConfig.instance2 -Audit LoginAudit | Read-DbaAuditFile -Raw
47+
$results | Should -Not -BeNullOrEmpty
4848
}
4949
It "returns some results" {
5050
$results = Get-DbaInstanceAudit -SqlInstance $TestConfig.instance2 -Audit LoginAudit | Read-DbaAuditFile | Select-Object -First 1
51-
$results.server_principal_name | Should -Not -Be $null
51+
$results.server_principal_name | Should -Not -BeNullOrEmpty
5252
}
5353
}
5454
}

tests/Read-DbaXEFile.Tests.ps1

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
1616

1717
Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
1818
Context "Verifying command output" {
19-
# THIS WORKS, I SWEAR
2019
It "returns some results" {
21-
$results = Get-DbaXESession -SqlInstance $TestConfig.instance2 | Read-DbaXEFile -Raw -WarningAction SilentlyContinue
22-
[System.Linq.Enumerable]::Count($results) -gt 1 | Should Be $true
20+
$results = Get-DbaXESession -SqlInstance $TestConfig.instance2 | Read-DbaXEFile -Raw
21+
$results | Should -Not -BeNullOrEmpty
2322
}
2423
It "returns some results" {
25-
$results = Get-DbaXESession -SqlInstance $TestConfig.instance2 | Read-DbaXEFile -WarningAction SilentlyContinue
26-
$results.Count -gt 1 | Should Be $true
24+
$results = Get-DbaXESession -SqlInstance $TestConfig.instance2 | Read-DbaXEFile
25+
$results | Should -Not -BeNullOrEmpty
2726
}
2827
}
2928
}

tests/Watch-DbaXESession.Tests.ps1

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,19 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
1313
}
1414
}
1515

16-
# This command is special and runs infinitely so don't actually try to run it
1716
Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
1817
Context "Command functions as expected" {
19-
It "warns if SQL instance version is not supported" {
20-
$results = Watch-DbaXESession -SqlInstance $TestConfig.instance1 -Session system_health -WarningAction SilentlyContinue -WarningVariable versionwarn
21-
$versionwarn -join '' -match "SQL Server version 11 required" | Should Be $true
18+
BeforeAll {
19+
Stop-DbaXESession -SqlInstance $TestConfig.instance2 -Session system_health -EnableException -Confirm:$false
20+
}
21+
AfterAll {
22+
Start-DbaXESession -SqlInstance $TestConfig.instance2 -Session system_health -EnableException -Confirm:$false
23+
}
24+
25+
# This command is special and runs infinitely so don't actually try to run it
26+
It "warns if XE session is not running" {
27+
$results = Watch-DbaXESession -SqlInstance $TestConfig.instance2 -Session system_health -WarningAction SilentlyContinue -WarningVariable warn
28+
$warn | Should -Match 'system_health is not running'
2229
}
2330
}
2431
}

tests/pester.groups.ps1

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ $TestsRunGroups = @{
3939
# tests that fail because the command does not work
4040
'Copy-DbaDbCertificate',
4141
'Export-DbaDacPackage',
42-
'Read-DbaAuditFile',
43-
'Read-DbaXEFile',
4442
# takes too long
4543
'Install-DbaSqlWatch',
4644
'Uninstall-DbaSqlWatch',

0 commit comments

Comments
 (0)