PowerShell script exchange
Post Reply
ithark
Influencer
Posts: 22
Liked: 4 times
Joined: Sep 30, 2020 11:22 am
Full Name: Karthik
Contact:

Script to summarize tapes used for each job

Post by ithark »

Dear Guys,
We are running tape jobs every day in our environment. Every Monday, the tapes used for the last 7 days would be exported.
According to our policies, we need to know the name of the tapes each of the tape job used for the past 7 days.
Can this be achieved via powershell?

Thanks in advance!
Regards,
Karthik

Egor Yakovlev
Veeam Software
Posts: 2130
Liked: 526 times
Joined: Jun 14, 2013 9:30 am
Full Name: Egor Yakovlev
Location: Prague, Czech Republic
Contact:

Re: Script to summarize tapes used for each job

Post by Egor Yakovlev » 1 person likes this post

Hi Karthik

Can we look at it from the other side? Like "Show me all tapes used in last 7 days?" (with no care which job used them to write backups, you will have to export them all anyway)

Code: Select all

Get-VBRTapeMedium | Where-Object {$_.LastWriteTime.Date -gt (Get-Date).AddDays(-7)} | Sort-Object -Property LastWriteTime -Descending | Select-Object -Property Barcode, LastWriteTime | Format-list

ithark
Influencer
Posts: 22
Liked: 4 times
Joined: Sep 30, 2020 11:22 am
Full Name: Karthik
Contact:

Re: Script to summarize tapes used for each job

Post by ithark »

Hi @Egor
Appreciate your help.
I actually know how to get the used tapes for a period of time. I just need to know which jobs were written to those tapes.
This is mainly because of the fact that we will export the tapes to a safe and it is a mandatory clause that we have to document what is inside the tapes before we can save them in the safe.
Regards,
Karthik
Regards,
Karthik

Egor Yakovlev
Veeam Software
Posts: 2130
Liked: 526 times
Joined: Jun 14, 2013 9:30 am
Full Name: Egor Yakovlev
Location: Prague, Czech Republic
Contact:

Re: Script to summarize tapes used for each job

Post by Egor Yakovlev » 3 people like this post

I guess it can be done this way.

Quick notes:
- I assume you are using industry standard 8 symbols for barcode naming.
- I assume you still have jobs in a GUI. If you backup to tape, then delete the job - script will not yield sessions for it.

Code: Select all

$TapeJobs = Get-VBRTapeJob

Write-Host "You have"$TapeJobs.Count"tape jobs in total" -ForegroundColor Green
Foreach ($Job in $TapeJobs) {
    Clear-Variable -Name "UniqueTapes"
    $TapeJobSessionsInLast7Days = Get-VBRSession -Job $Job | Where-Object {$_.CreationTime -gt (Get-Date).AddDays(-7)} | Sort-Object -Property CreationTime
    if ($null -ne $TapeJobSessionsInLast7Days){
        Write-Host "`nJob" -NoNewline -ForegroundColor Magenta
        Write-Host " ["$Job.Name"] " -NoNewline -ForegroundColor Green
        Write-Host "was executed within last 7 days during following times:" -ForegroundColor Magenta
        foreach ($Session in $TapeJobSessionsInLast7Days) {
            $SessionAUXData = (Get-VBRTaskSession -Session $Session).JobSess.AuxData
            $SessionAUXDATA = "<root>"+$SessionAUXDATA+"</root>"                        
            [xml]$SessionData = $SessionAUXData
            $UsedTapeBarcode = $SessionData.root.TapeAuxData.TapeMediums.TapeMedium.Barcode | Select-Object -Unique
            Write-Host $Session.CreationTime "and used tape" $UsedTapeBarcode
            $UniqueTapes += $UsedTapeBarcode
        }
         $UniqueTapes = $UniqueTapes -replace '\s',''
         $UniqueTapes = $UniqueTapes -split "([a-z0-9]{8})"  | ?{ $_  }
         $UniqueTapes = $UniqueTapes | Select-Object -Unique
         Write-Host "Unique tapes used by this job in last 7 days:" $UniqueTapes -ForegroundColor Yellow
     }
    else {Write-Host "Your tape jobs didn't run in last 7 days!" -ForegroundColor Yellow}
}
Image

/Hope that helps!

ithark
Influencer
Posts: 22
Liked: 4 times
Joined: Sep 30, 2020 11:22 am
Full Name: Karthik
Contact:

Re: Script to summarize tapes used for each job

Post by ithark » 1 person likes this post

Hi @Egor
This is one of the reason why we love Veeam so much :-)
We are able to get the required details almost perfectly. I just have one last request.
Is there anyway we can use a filter to display the details of only the scheduled jobs? Or only the active jobs and to ignore the ones which are disabled?
Regards,
Karthik
Regards,
Karthik

Egor Yakovlev
Veeam Software
Posts: 2130
Liked: 526 times
Joined: Jun 14, 2013 9:30 am
Full Name: Egor Yakovlev
Location: Prague, Czech Republic
Contact:

Re: Script to summarize tapes used for each job

Post by Egor Yakovlev » 1 person likes this post

Sure, just apply filter in the first line, where you retrieve the jobs:

Code: Select all

$TapeJobs = Get-VBRTapeJob | Where-Object {($_.Enabled -eq $True) -and ($_.NextRun -ne $null)}
will pull out only Enabled jobs with Schedule set(NextRun date is present).

/Cheers!

ithark
Influencer
Posts: 22
Liked: 4 times
Joined: Sep 30, 2020 11:22 am
Full Name: Karthik
Contact:

Re: Script to summarize tapes used for each job

Post by ithark » 3 people like this post

Это просто идеально (That's just perfect!)
Thank you @Egor
Regards,
Karthik

Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests