PowerShell script exchange
Post Reply
Thomas_
Novice
Posts: 6
Liked: never
Joined: Sep 17, 2021 9:05 am
Contact:

Export Tape Details after Job

Post by Thomas_ »

Hi,

we have an internal application to create the tape covers with tape name and media set name on it.
We add at the moment the name of the media set manually into the application to the used tape from the mail report after the backup job.

Is it possible with a powershell script to extract tape name and media set name from the database with a "post-job"-script? I would write this either into another SQL database or in a csv/txt/whatever file.

Regards,
Thomas
david.domask
Veeam Software
Posts: 2123
Liked: 513 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Export Tape Details after Job

Post by david.domask »

Hi @Thomas_,

There is a way of doing this that has a few goofy steps, but it's quite simple with a function.

We need to get the Tape Session data with Get-VBRSession; the real info we need is actually on another object, but you'll see the workflow below:

Code: Select all

$Job = Get-VBRTapeJob -Name 'name of the job'
$TSess = Get-VBRSession -Job $Job | Sort-Object -Property CreationTime -Descending | Select -First 1 #this gets us most recent session
$BSess = Get-VBRBackupSession -Id $Tsess.Id
$BSess.AuxData #This returns an XML output that will include the used tapes in a session if any. If there is no TapeMediums node, then it means the job didn't use any tapes for some reason, and should be checked. Normally it's just due to scheduling issue and there was nothing to backup per the schedule, but it can be other things.
Get-VBRTapeMedium -Id [Id obtained from the $BSess.AuxData]
We get the session twice for two reasons:

1. Get-VBRSession is more convenient as it allows us to pass a Job Object and get its sesssions. However, this doesn't have the AuxData property.
2. Get-VBRBackupSession _does_ return the AuxData property, so we just use the $TSess.Id to get the resulting object from Get-VBRBackupSession. It should be a pretty fast lookup.

The auxdata output should look something like this:

Code: Select all

PS C:\Users\Administrator> $BSess.AuxData
<TapeAuxData TerminatedByUser="False" WillBeRetried="False"><TapeLibrary Name="vtl2 (DDOM-VEEAM-RB4)" /><TapeMediums><TapeMedium Barcode="vtl104L5" Name="vtl104L5" Id="9c2ed1a5-42fa-46c9-a251-55b5e85e06de" /></TapeMediums><CBackupSessionWorkDetails><WorkDuration>294847934587</WorkDuration></CBackupSessionWorkDetails><SessionStarterInfo Version="1" UserName="SYSTEM" UserDomainName="NT AUTHORITY" RunManually="False" LoginType="4" LoginName="SYSTEM" /></TapeAuxData>
The <TapeMediums> node is what you want. Powershell has automatic XML parsing, so cast the data as [xml] and you can do much easier parsing:

Code: Select all

PS C:\Users\Administrator> [xml]$TapeAux = $BSess.AuxData
PS C:\Users\Administrator> $TapeAux.TapeAuxData
TerminatedByUser          : False
WillBeRetried             : False
TapeLibrary               : TapeLibrary
TapeMediums               : TapeMediums
CBackupSessionWorkDetails : CBackupSessionWorkDetails
SessionStarterInfo        : SessionStarterInfo

PS C:\Users\Administrator> $TapeAux.TapeAuxData.TapeMediums.TapeMedium

Barcode  Name     Id
-------  ----     --
vtl104L5 vtl104L5 9c2ed1a5-42fa-46c9-a251-55b5e85e06de
The [xml] casts the data in $TapeAux as XML, and PS will handle the rest. Ultimately you will parse out the ID(s) from $TapeAux.TapeAuxData.TapeMediums.TapeMedium.Id, and pass all of those to Get-VBRTapeMedium -Id. This will let you check tape barcode, name, and media set and tie it to a given backup session.

Sorry for the wall of text; I know it seems like a lot, but really you just want 1-2 functions and then pass your tape jobs through the two functions.

As for running it after a job, there are _ways_ of making a post-job script find which job executed it, but I advise maybe just run a daily report on the tapes after the tape jobs are run. If it really must be triggered by a post-job script, you can use the following code to find the job that invoked the script:

Code: Select all

$ScriptPID = Get-WMIObject win32_process | Where-Object {$_.ProcessID -eq $PID}
$ParentProc = Get-WMIObject win32_process | Where-Object {$_.ProcessID -eq $ScriptPID.ParentProcessId}
$JobID = $ParentProc.CommandLine.Split()[7].Trim('"')
$Job = Get-VBRJob | Where-Object {$_.id -eq $JobID}
$job will be populated with the results of Get-VBRJob, and you can just jump into the rest of the script there. But I do advise against this, this trick might not work in future versions (I doubt it will change, but who knows)
David Domask | Product Management: Principal Analyst
Thomas_
Novice
Posts: 6
Liked: never
Joined: Sep 17, 2021 9:05 am
Contact:

Re: Export Tape Details after Job

Post by Thomas_ »

Hi David,

thanks for the detailed instruction, I will give it a try!
Post Reply

Who is online

Users browsing this forum: No registered users and 25 guests