Skip to content
Merged
Show file tree
Hide file tree
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
27 changes: 19 additions & 8 deletions private/functions/Start-DbccCheck.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function Start-DbccCheck {
)

$servername = $server.name
$escapedDbName = $DbName.Replace("]", "]]")

if ($Pscmdlet.ShouldProcess($sourceserver, "Running dbcc check on $DbName on $servername")) {
if ($server.ConnectionContext.StatementTimeout -ne 0) {
Expand All @@ -17,17 +18,24 @@ function Start-DbccCheck {
try {
if ($table) {
$null = $server.databases[$DbName].CheckTables('None')
Write-Verbose "Dbcc CheckTables finished successfully for $DbName on $servername"
Write-Verbose "DBCC CheckTables finished successfully for $DbName on $servername"
return [PSCustomObject]@{
Status = "Success"
Output = $null
}
} else {
if ($MaxDop) {
$null = $server.Query("DBCC CHECKDB ([$DbName]) WITH MAXDOP = $MaxDop")
Write-Verbose "Dbcc CHECKDB finished successfully for $DbName on $servername"
$query = "DBCC CHECKDB ([$escapedDbName]) WITH MAXDOP = $MaxDop"
} else {
$null = $server.Query("DBCC CHECKDB ([$DbName])")
Write-Verbose "Dbcc CHECKDB finished successfully for $DbName on $servername"
$query = "DBCC CHECKDB ([$escapedDbName])"
}
$dbccOutput = Invoke-DbaQuery -SqlInstance $server -Query $query -MessagesToOutput -EnableException
Write-Verbose "DBCC CHECKDB finished successfully for $DbName on $servername"
return [PSCustomObject]@{
Status = "Success"
Output = $dbccOutput
}
}
return "Success"
} catch {
$originalException = $_.Exception
$loopNo = 0
Expand Down Expand Up @@ -61,7 +69,10 @@ function Start-DbccCheck {
} catch {
$null
}
return $message.Trim()
return [PSCustomObject]@{
Status = $message.Trim()
Output = $null
}
}
}
}
}
29 changes: 18 additions & 11 deletions public/Test-DbaLastBackup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ function Test-DbaLastBackup {
- DbccStart (datetime) - Date and time when the DBCC CHECKDB operation started
- DbccEnd (datetime) - Date and time when the DBCC CHECKDB operation ended
- DbccElapsed (timespan as string) - Formatted duration of the DBCC CHECKDB operation (HH:mm:ss format)
- DbccOutput (string array) - Detailed informational messages returned by DBCC CHECKDB (row counts, Service Broker analysis, etc.); $null when skipped
- BackupDates (datetime array) - Array of backup start times for all backup files in the restore chain
- BackupFiles (string array) - Array of backup file paths used for the restore operation

Expand Down Expand Up @@ -422,6 +423,7 @@ function Test-DbaLastBackup {
DbccStart = $null
DbccEnd = $null
DbccElapsed = $null
DbccOutput = $null
BackupDates = $null
BackupFiles = $null
}
Expand All @@ -445,6 +447,7 @@ function Test-DbaLastBackup {
DbccStart = $null
DbccEnd = $null
DbccElapsed = $null
DbccOutput = $null
BackupDates = [dbadatetime[]]($lastbackup.Start)
BackupFiles = $lastbackup.FullName
}
Expand Down Expand Up @@ -491,7 +494,7 @@ function Test-DbaLastBackup {
[PSCustomObject]@{
SourceServer = $source
TestServer = $destination
Database = $db.name
Database = $dbName
FileExists = $false
Size = $null
RestoreResult = "Skipped"
Expand All @@ -503,6 +506,7 @@ function Test-DbaLastBackup {
DbccStart = $null
DbccEnd = $null
DbccElapsed = $null
DbccOutput = $null
BackupDates = $null
BackupFiles = $null
}
Expand Down Expand Up @@ -537,9 +541,9 @@ function Test-DbaLastBackup {
$sourcerealname = $sourceserver.ComputerNetBiosName
$destrealname = $destserver.ComputerNetBiosName

if ($BackupFolder) {
if ($BackupFolder.StartsWith("\\") -eq $false -and $sourcerealname -ne $destrealname) {
Stop-Function -Message "Backup folder must be a network share if the source and destination servers are not the same." -Continue
if ($CopyPath) {
if ($CopyPath.StartsWith("\\") -eq $false -and $sourcerealname -ne $destrealname) {
Stop-Function -Message "CopyFolder must be a network share if the source and destination servers are not the same." -Continue
}
}
}
Expand All @@ -565,11 +569,10 @@ function Test-DbaLastBackup {
}

if ((Test-Bound -ParameterName StorageCredential) -and (Test-Bound -ParameterName CopyFile)) {
Stop-Function -Message "Cannot use CopyFile with cloud storage backups (Azure/S3), set to false." -Continue
$CopyFile = $false
Stop-Function -Message "Cannot use CopyFile with cloud storage backups (Azure/S3)." -Continue
}

Write-Message -Level Verbose -Message "Getting recent backup history for $($db.Name) on $instance."
Write-Message -Level Verbose -Message "Getting recent backup history for $dbName on $instance."

if (Test-Bound "IgnoreLogBackup") {
Write-Message -Level Verbose -Message "Skipping Log backups as requested."
Expand All @@ -596,7 +599,7 @@ function Test-DbaLastBackup {
[PSCustomObject]@{
SourceServer = $source
TestServer = $destination
Database = $db.name
Database = $dbName
FileExists = $null
Size = [dbasize](($lastbackup.TotalSize | Measure-Object -Sum).Sum)
RestoreResult = "The backup size for $dbName ($totalSizeMB MB) exceeds the specified maximum size ($MaxSize MB)."
Expand All @@ -608,6 +611,7 @@ function Test-DbaLastBackup {
DbccStart = $null
DbccEnd = $null
DbccElapsed = $null
DbccOutput = $null
BackupDates = [dbadatetime[]]($lastbackup.Start)
BackupFiles = $lastbackup.FullName
}
Expand Down Expand Up @@ -652,7 +656,7 @@ function Test-DbaLastBackup {
}
$copysuccess = $true
} catch {
Write-Message -Level Warning -Message "Failed to copy backups for $dbName on $instance to $destdirectory - $_."
Write-Message -Level Warning -Message "Failed to copy backups for $dbName on $instance - $_."
$copysuccess = $false
}
}
Expand Down Expand Up @@ -727,7 +731,7 @@ function Test-DbaLastBackup {
$dbccresult = $null
$success = $null
$errormsg = $null
$dbccElapsed = $restoreElapsed = $startRestore = $endRestore = $startDbcc = $endDbcc = $null
$dbccElapsed = $restoreElapsed = $startRestore = $endRestore = $startDbcc = $endDbcc = $dbccOutput = $null

if ($workItem.SkipRestoreResult) {
$success = $workItem.SkipRestoreResult
Expand Down Expand Up @@ -820,7 +824,9 @@ function Test-DbaLastBackup {
Write-Message -Level Verbose -Message "Starting DBCC."

$startDbcc = Get-Date
$dbccresult = Start-DbccCheck -Server $destserver -DbName $prefixedDbName -MaxDop $MaxDop 3>$null
$dbccCheckResult = Start-DbccCheck -Server $destserver -DbName $prefixedDbName -MaxDop $MaxDop 3>$null
$dbccresult = $dbccCheckResult.Status
$dbccOutput = $dbccCheckResult.Output
$endDbcc = Get-Date

$dbccts = New-TimeSpan -Start $startDbcc -End $endDbcc
Expand Down Expand Up @@ -887,6 +893,7 @@ function Test-DbaLastBackup {
DbccStart = [dbadatetime]$startDbcc
DbccEnd = [dbadatetime]$endDbcc
DbccElapsed = $dbccElapsed
DbccOutput = $dbccOutput
BackupDates = [dbadatetime[]]($lastbackup.Start)
BackupFiles = $lastbackup.FullName
}
Expand Down
Loading