Created
June 21, 2018 11:54
-
-
Save SQLDBAWithABeard/0ec91f1d695fe45d2fc2469b6c081c03 to your computer and use it in GitHub Desktop.
Restore-DbaDatabase, timings and error handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$file = ## whatever Get-ChildItem or path | |
$Instance | |
$JobName = 'Restore' + (Get-Random).ToString() | |
$Date = (Get-Date).DateTime.ToString() | |
$msg = "$Date - Starting Job $JobName to restore the database" | |
## LoggingFunction $msg | |
$pscmdlet.WriteVerbose($msg) | |
Start-Job -Name $JobName -ScriptBlock {$Using:File | Restore-DbaDatabase -SqlInstance $Using:Instance -ReuseSourceFolderStructure -WithReplace -EnableException -AllowContinue -Verbose:$false } | |
While ((Get-Job $JobName).State -ne 'Completed') { | |
Start-Sleep -Seconds 15 | |
$Query = "SELECT | |
DER.SESSION_ID as SPID, | |
RTRIM(SP.Loginame), | |
RTRIM(SP.nt_domain) as Domain, | |
RTRIM(SP.nt_username) as NTUserName, | |
'[' + CAST(DER.DATABASE_ID AS VARCHAR(10)) + '] ' + DB_NAME(DER.DATABASE_ID) AS [Database], | |
DER.PERCENT_COMPLETE as 'PercentComplete', DER.START_TIME, DER.STATUS, DER.COMMAND, | |
DATEADD(MS, DER.ESTIMATED_COMPLETION_TIME, GETDATE()) AS EST_COMP, | |
DER.CPU_TIME | |
FROM SYS.DM_EXEC_REQUESTS DER | |
left join | |
sys.sysprocesses SP | |
on DER.Session_id = SP.spid | |
WHERE COMMAND LIKE '%RESTORE%'" | |
$RestoreProgress = Invoke-DbaSqlQuery -SqlInstance $Instance -Database master -Query $Query | |
if($RestoreProgress){ | |
$PercentComplete = $RestoreProgress[0].PercentComplete | |
$CompleteEstimate = $RestoreProgress[0].EST_COMP | |
} | |
else{ | |
$PercentComplete = 'Unknown' | |
$CompleteEstimate = 'Unknown' | |
} | |
$Date = (Get-Date).DateTime.ToString() | |
$msg = "$Date - Currently $PercentComplete % completed restoring database $DBName and is estimated to complete at $CompleteEstimate" | |
## LoggingFunction $msg | |
$pscmdlet.WriteVerbose($msg) | |
Start-Sleep -Seconds 15 | |
} | |
$Date = (Get-Date).DateTime.ToString() | |
$msg = "$Date - Restore Job has completed" | |
## LoggingFunction $msg | |
$pscmdlet.WriteVerbose($msg) | |
$job = Get-Job $JobName -IncludeChildJob | |
if ($job.Error) { | |
$Jmsg = ($job.Error | Out-String).Replace("'","''") | |
$Date = (Get-Date).DateTime.ToString() | |
$msg = "$Date -The Restore Job completed with errors - $Jmsg" | |
## LoggingFunction $msg | |
$pscmdlet.WriteWarning($msg) | |
Return $false | |
} | |
if ($job.Warning) { | |
$Jmsg = ($job.Warning | Out-String).Replace("'","''") | |
$Date = (Get-Date).DateTime.ToString() | |
$msg = "$Date -The Restore Job completed with warnings which probably means the restore failed - $Jmsg" | |
## LoggingFunction $msg | |
$pscmdlet.WriteWarning($msg) | |
Return $false | |
} | |
if($job.childjobs.Output.Restorecomplete -ne $true){ | |
$Jmsg = ( $job.childjobs.Output.ExitError | Select * | Out-String).Replace("'","''") | |
$Date = (Get-Date).DateTime.ToString() | |
$msg = "$Date - The Restore Job completed without restoring the databases - $Jmsg" | |
## LoggingFunction -DBAStackMessageType ERROR | |
$pscmdlet.WriteWarning($msg) | |
Return $false | |
} | |
else{ | |
$Date = (Get-Date).DateTime.ToString() | |
$msg = "$Date - The Restore Job completed successfully" | |
## LoggingFunction $msg | |
$pscmdlet.WriteWarning($msg) | |
} | |
Get-Job $JobName | Remove-Job | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment