Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start-DbccCheck, better extraction of errors #9624

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions private/functions/Start-DbccCheck.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function Start-DbccCheck {
$servername = $server.name

if ($Pscmdlet.ShouldProcess($sourceserver, "Running dbcc check on $DbName on $servername")) {
if ($server.ConnectionContext.StatementTimeout = 0 -ne 0) {
if ($server.ConnectionContext.StatementTimeout -ne 0) {
$server.ConnectionContext.StatementTimeout = 0
}

Expand All @@ -29,14 +29,34 @@ function Start-DbccCheck {
}
return "Success"
} catch {
$message = $_.Exception
if ($null -ne $_.Exception.InnerException) { $message = $_.Exception.InnerException }
$originalException = $_.Exception
$loopNo = 0
while ($loopNo -ne 5) {
$loopNo ++
if ($null -ne $originalException.InnerException) {
$originalException = $originalException.InnerException
} else {
break
}
}
$message = $originalException.ToString()

# english cleanup only sorry
try {
$newmessage = ($message -split "at Microsoft.SqlServer.Management.Common.ConnectionManager.ExecuteTSql")[0]
$newmessage = ($newmessage -split "Microsoft.SqlServer.Management.Common.ExecutionFailureException:")[1]
$newmessage = ($newmessage -replace "An exception occurred while executing a Transact-SQL statement or batch. ---> Microsoft.Data.SqlClient.SqlException:").Trim()
$newmessage = $message
if ($newmessage -like '*at Microsoft.SqlServer.Management.Common.ConnectionManager.ExecuteTSql*') {
$newmessage = ($newmessage -split "at Microsoft.SqlServer.Management.Common.ConnectionManager.ExecuteTSql")[0]
}
if ($newmessage -like '*Microsoft.SqlServer.Management.Common.ExecutionFailureException:*') {
$newmessage = ($newmessage -split "Microsoft.SqlServer.Management.Common.ExecutionFailureException:")[1]
}
if ($newmessage -like '*An exception occurred while executing a Transact-SQL statement or batch. ---> Microsoft.Data.SqlClient.SqlException:*') {
$newmessage = ($newmessage -replace "An exception occurred while executing a Transact-SQL statement or batch. ---> Microsoft.Data.SqlClient.SqlException:").Trim()
}
if ($newmessage -like '*An exception occurred while executing a Transact-SQL statement or batch*') {
$newmessage = ($newmessage -split "An exception occurred while executing a Transact-SQL statement or batch")[1]
}

$message = $newmessage
} catch {
$null
Expand Down