Skip to main content

Get last SQL transaction log session


StefanZi
Forum|alt.badge.img+3

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

 

6 comments

Chris.Childerhose
Forum|alt.badge.img+21
  • Veeam Legend, Veeam Vanguard
  • 8549 comments
  • October 17, 2022

I find these very interesting thanks for sharing.  Seeing PowerShell is always cool. 😎


StefanZi
Forum|alt.badge.img+3
  • Author
  • Comes here often
  • 49 comments
  • October 17, 2022

@Kseniya just noticed that there’s no syntax highlighting for PowerShell when adding inline code - maybe this is something that can be added here?!


mkevenaar
Forum|alt.badge.img+15
  • Veeam Vanguard
  • 149 comments
  • October 17, 2022
StefanZi wrote:

@Kseniya just noticed that there’s no syntax highlighting for PowerShell when adding inline code - maybe this is something that can be added here?!

Ksenia is no longer working for Veeam, tagging @Madi.Cristil


StefanZi
Forum|alt.badge.img+3
  • Author
  • Comes here often
  • 49 comments
  • October 17, 2022

Oops, thanks for correcting it .. I wasn’t too active in the community the last time. Shame on me.


Madi.Cristil
Forum|alt.badge.img+8
  • Community Manager
  • 618 comments
  • October 17, 2022

Hi @StefanZi :) Nice to meet you first of all! Let me know how can I help. If anything you can also write me on Teams!


mkevenaar
Forum|alt.badge.img+15
  • Veeam Vanguard
  • 149 comments
  • October 17, 2022
StefanZi wrote:

Oops, thanks for correcting it .. I wasn’t too active in the community the last time. Shame on me.

No worries, we started the Automation Desk today, perhaps you are interested in that too?


Comment