Skip to content

Commit b45f06a

Browse files
authored
Start-DbccCheck, better extraction of errors (#9624)
1 parent 8192df4 commit b45f06a

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

private/functions/Start-DbccCheck.ps1

+26-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function Start-DbccCheck {
1010
$servername = $server.name
1111

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

@@ -29,14 +29,34 @@ function Start-DbccCheck {
2929
}
3030
return "Success"
3131
} catch {
32-
$message = $_.Exception
33-
if ($null -ne $_.Exception.InnerException) { $message = $_.Exception.InnerException }
32+
$originalException = $_.Exception
33+
$loopNo = 0
34+
while ($loopNo -ne 5) {
35+
$loopNo ++
36+
if ($null -ne $originalException.InnerException) {
37+
$originalException = $originalException.InnerException
38+
} else {
39+
break
40+
}
41+
}
42+
$message = $originalException.ToString()
3443

3544
# english cleanup only sorry
3645
try {
37-
$newmessage = ($message -split "at Microsoft.SqlServer.Management.Common.ConnectionManager.ExecuteTSql")[0]
38-
$newmessage = ($newmessage -split "Microsoft.SqlServer.Management.Common.ExecutionFailureException:")[1]
39-
$newmessage = ($newmessage -replace "An exception occurred while executing a Transact-SQL statement or batch. ---> Microsoft.Data.SqlClient.SqlException:").Trim()
46+
$newmessage = $message
47+
if ($newmessage -like '*at Microsoft.SqlServer.Management.Common.ConnectionManager.ExecuteTSql*') {
48+
$newmessage = ($newmessage -split "at Microsoft.SqlServer.Management.Common.ConnectionManager.ExecuteTSql")[0]
49+
}
50+
if ($newmessage -like '*Microsoft.SqlServer.Management.Common.ExecutionFailureException:*') {
51+
$newmessage = ($newmessage -split "Microsoft.SqlServer.Management.Common.ExecutionFailureException:")[1]
52+
}
53+
if ($newmessage -like '*An exception occurred while executing a Transact-SQL statement or batch. ---> Microsoft.Data.SqlClient.SqlException:*') {
54+
$newmessage = ($newmessage -replace "An exception occurred while executing a Transact-SQL statement or batch. ---> Microsoft.Data.SqlClient.SqlException:").Trim()
55+
}
56+
if ($newmessage -like '*An exception occurred while executing a Transact-SQL statement or batch*') {
57+
$newmessage = ($newmessage -split "An exception occurred while executing a Transact-SQL statement or batch")[1]
58+
}
59+
4060
$message = $newmessage
4161
} catch {
4262
$null

0 commit comments

Comments
 (0)