Skip to content

Test-DbaLastBackup - Add DbccOutput property with detailed DBCC messages#10239

Open
andreasjordan wants to merge 4 commits intodevelopmentfrom
claude/issue-5375-20260314-1614
Open

Test-DbaLastBackup - Add DbccOutput property with detailed DBCC messages#10239
andreasjordan wants to merge 4 commits intodevelopmentfrom
claude/issue-5375-20260314-1614

Conversation

@andreasjordan
Copy link
Collaborator

@andreasjordan andreasjordan commented Mar 14, 2026

Implements the feature requested in #5375: capture and expose the detailed informational messages from DBCC CHECKDB as a new DbccOutput property on every output object returned by Test-DbaLastBackup.

Also fixes a bracket-escaping bug in Start-DbccCheck where database names containing ] would cause a SQL syntax error.

Closes #5375

Generated with Claude Code

This might need some additional commits:

  • Write "Dbcc" as "DBCC" (always uppercase)

- Modify Start-DbccCheck to return a structured object with Status and Output
  properties instead of a bare string. DBCC CHECKDB informational messages
  (row counts, Service Broker analysis, etc.) are captured via
  Invoke-DbaQuery -MessagesToOutput and returned in the Output property.
- Fix bracket-escaping bug in Start-DbccCheck: database names containing ']'
  are now escaped to ']]' before being used in the DBCC CHECKDB query.
- Update Test-DbaLastBackup to consume the new structured return value and
  expose the captured messages as a new DbccOutput property on every output
  object. DbccOutput is $null when DBCC was skipped.

Closes #5375

(do Test-DbaLastBackup)

Co-authored-by: Andreas Jordan <andreasjordan@users.noreply.github.com>
@andreasjordan
Copy link
Collaborator Author

I will wait with the work on this until #10241 is merged to avoid conflicts.

@andreasjordan
Copy link
Collaborator Author

@claude please review this pull request but also review the function in general.

@claude
Copy link

claude bot commented Mar 20, 2026

Claude finished @andreasjordan's task in 1m 52s —— View job


PR Review: Test-DbaLastBackup - DbccOutput + General Function Review

  • Gather context and read changed files
  • Review Start-DbccCheck.ps1 changes
  • Review Test-DbaLastBackup.ps1 changes + general function review
  • Post final review feedback

Overall Assessment

The feature (exposing DBCC messages) and the bracket-escaping fix are both good ideas. However, there is one critical bug introduced by the PR, plus a few other issues worth addressing.


Critical Bug

$dbName vs $prefixedDbName in Start-DbccCheck call

File: public/Test-DbaLastBackup.ps1, line 826

The diff changes the Start-DbccCheck call from:

$dbccresult = Start-DbccCheck -Server $destserver -DbName $prefixedDbName -MaxDop $MaxDop 3>$null

to:

$dbccCheckResult = Start-DbccCheck -Server $destserver -DbName $dbName -MaxDop $MaxDop 3>$null

This is incorrect. The database is restored under the prefixed name ($prefixedDbName = "$prefix$dbName", e.g. dbatools-testrestore-MyDB). DBCC CHECKDB must run against $prefixedDbName — running it against $dbName would either target the live production database or fail because that name doesn't exist on the destination. This change must be reverted: -DbName $prefixedDbName.

Fix this →


Other Issues

1. $dbccOutput not initialized — potential leak across iterations

File: public/Test-DbaLastBackup.ps1, line 733

The multi-null assignment at the top of the work-item loop is:

$dbccElapsed = $restoreElapsed = $startRestore = $endRestore = $startDbcc = $endDbcc = $null

$dbccOutput is not reset here. If database A runs successfully and populates $dbccOutput, then database B is skipped (NoCheck, VerifyOnly, master, or failed restore), $dbccOutput will still contain database A's messages in database B's output row. Add $dbccOutput to the chain:

$dbccElapsed = $restoreElapsed = $startRestore = $endRestore = $startDbcc = $endDbcc = $dbccOutput = $null

Fix this →

2. Missing DbccOutput in -Path flow early-exit objects

File: public/Test-DbaLastBackup.ps1, lines 411–428 and 434–451

The PR correctly adds DbccOutput = $null to the InputObject flow early-exit objects (lines 507 and 613), but the two equivalent early-exit objects in the -Path flow are missing it:

  • "No full backup found" block (~line 411)
  • "MaxSize exceeded" block (~line 434)

All output objects should have a consistent schema. Both blocks need DbccOutput = $null.

Fix this →

3. Verbose message casing (noted in PR description)

File: private/functions/Start-DbccCheck.ps1, lines 21 and 33

As mentioned in the PR description, "Dbcc" should be "DBCC":

Write-Verbose "Dbcc CheckTables finished successfully..."  # → "DBCC CheckTables..."
Write-Verbose "Dbcc CHECKDB finished successfully..."      # → "DBCC CHECKDB..."

Positive Changes

  • Bracket-escaping fix ($DbName.Replace("]", "]]")) is correct and important — database names with ] would have caused a SQL syntax error.
  • Structured return object from Start-DbccCheck (Status/Output properties) is a clean design improvement over returning a bare string.
  • Invoke-DbaQuery -MessagesToOutput is the right approach to capture DBCC informational messages.
  • $table branch correctly gets a proper return object now — in the original code it returned nothing, which would have caused the .Status access to fail.
  • .OUTPUTS documentation is updated to include the new DbccOutput property.

General Function Review Notes

Beyond the PR changes, a few observations on Test-DbaLastBackup.ps1 as a whole:

  • Line 542 references $BackupFolder which is not a declared parameter — this appears to be dead/unreachable code from an old parameter that was renamed to $CopyPath.
  • Line 641 logs $destdirectory which is also not defined in scope — this verbose message would emit an empty value.
  • Line 570–571: Stop-Function -Continue followed by $CopyFile = $false — the assignment after Stop-Function -Continue is unreachable.
  • Lines 597–617 vs 432–453: The MaxSize exceeded objects have slightly different structure (InputObject path includes DbccOutput, Path path does not — addressed above).

|

@andreasjordan
Copy link
Collaborator Author

@claude have I addressed all issues in my last commit?

@claude
Copy link

claude bot commented Mar 20, 2026

Claude finished @andreasjordan's task in 1m 0s —— View job


Checking if all issues have been addressed in the last commit...

  • Read changed files
  • Check each issue from previous review

Verification Results

Yes — all four issues from the previous review have been fixed:

Issue Status
Critical bug: Start-DbccCheck called with $dbName instead of $prefixedDbName ✅ Fixed — line 827 now uses $prefixedDbName
$dbccOutput not reset between loop iterations ✅ Fixed — line 734 includes $dbccOutput = $null in the reset chain
Missing DbccOutput in -Path flow early-exit objects (no-full-backup + MaxSize blocks) ✅ Fixed — both blocks (lines 426 and 450) now have DbccOutput = $null
Verbose message casing: "Dbcc" → "DBCC" ✅ Fixed — both verbose messages in Start-DbccCheck.ps1 (lines 21 and 33) now use DBCC

The PR looks good to merge.


@andreasjordan andreasjordan marked this pull request as ready for review March 20, 2026 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test-DbaLastBackup - Add info to output messages, or as part of the properties returned.

1 participant