How to get the start and end time of the last SQL log backup session?
I just got this question from one of my customers and it’s not soo easy to get there, so I thought it might be useful to share a basic example.
You’ll have to get the working task session and read & parse the logs for more details.
$sql_base_job = "My SQL Server Job"
$j = Get-VBRJob -Name $sql_base_job
$SQLJob = $j.FindChildSqlLogBackupJob()
$session = $SQLJob.FindLastSession()
# Tasksessions will be per machine, I only have one
$taskSession = Get-VBRTaskSession -Session $session
$logBackupLogs = $taskSession.Logger.GetLog().UpdatedRecords
$lastSessionStartLog = $logBackupLogs | ? { $_.Title.Contains("New transaction log backup interval started") } | Select -Last 1
$lastSessionEndLog = $logBackupLogs | ? { $_.Title.Contains("Transaction log backup completed") } | Select -Last 1
# Sanity check (Endtime > Starttime) might be good if a session was started but not yet finished
Write-Output "Last log session start: " $lastSessionStartLog.StartTime
Write-Output "Last log session stop: " $lastSessionEndLog.StartTime