diff --git a/private/functions/Start-DbccCheck.ps1 b/private/functions/Start-DbccCheck.ps1 index 9bdd800a37..b683fd7743 100644 --- a/private/functions/Start-DbccCheck.ps1 +++ b/private/functions/Start-DbccCheck.ps1 @@ -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 } @@ -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