PowerShell script exchange
Post Reply
mateus.kyn
Novice
Posts: 3
Liked: never
Joined: Mar 23, 2023 5:58 pm
Full Name: Mateus Gomes
Contact:

Report Year/month

Post by mateus.kyn »

Dear Sirs, good afternoon

I would like to ask for your help, looking here I found a very interesting script that lists for us all the backups that are made.

But I wanted to know if there is a way to put a parameter in it so that
I can insert specific dates to generate the report.
I believe this would be very interesting for everyone.
For it is something that for auditing is of utmost importance
I put the script below here.

Basically I wanted to know if we could put a parameter
Of dates
For example
Date 01/11/2021 to 01/11/2022

Code: Select all

add-PSSnapin "VeeamPSSnapIn" -ErrorAction SilentlyContinue
 

$HourstoCheck = 100000

$tempArry =@()
$tempArry = Get-VBRBackupSession
$allSesh = $allSesh+ $tempArry 

$tempArry =@()
$tempArry = @(Get-VBRJob | ? {$_.JobType -eq "Backup"})
$allJobsBk = $allJobsBk + $tempArry
# Gather all Backup sessions within timeframe
$seshListBk = @($allSesh | ?{($_.CreationTime -ge (Get-Date).AddHours(-$HourstoCheck)) -and $_.JobType -eq "Backup"})

# Get Backup session information
$totalxferBk = 0
$totalReadBk = 0
$seshListBk | %{$totalxferBk += $([Math]::Round([Decimal]$_.Progress.TransferedSize/1GB, 2))}
$seshListBk | %{$totalReadBk += $([Math]::Round([Decimal]$_.Progress.ReadSize/1GB, 2))}
$tempSeshListBk = $seshListBk
$seshListBk = @()
Foreach($job in (Get-VBRJob | ? {$_.JobType -eq "Backup"})) {
$seshListBk += $TempSeshListBk | ?{$_.Jobname -eq $job.name} | Sort-Object CreationTime -Descending | Select-Object -First 10
}

$successSessionsBk = @($seshListBk | ?{$_.Result -eq "Success"})
$warningSessionsBk = @($seshListBk | ?{$_.Result -eq "Warning"})
$failsSessionsBk = @($seshListBk | ?{$_.Result -eq "Failed"})
$runningSessionsBk = @($allSesh | ?{$_.State -eq "Working" -and $_.JobType -eq "Backup"})
$failedSessionsBk = @($seshListBk | ?{($_.Result -eq "Failed") -and ($_.WillBeRetried -ne "True")})

$All = @($seshListBk)



$bodySessSuccBk = $All | Sort Name, Creationtime | Select @{Name="Job Name"; Expression = {$_.Name}},
                @{Name="Start Time"; Expression = {$_.CreationTime}},
                @{Name="Stop Time"; Expression = {$_.EndTime}},
@{Name="Duration (Mins)"; Expression = {[Math]::Round($_.WorkDetails.WorkDuration.TotalMinutes,2)}},
@{Name="Avg Speed (MB/s)"; Expression = {[Math]::Round($_.Info.Progress.AvgSpeed/1MB,2)}},
@{Name="Total (GB)"; Expression = {[Math]::Round($_.Info.Progress.ProcessedSize/1GB,2)}},
@{Name="Processed (GB)"; Expression = {[Math]::Round($_.Info.Progress.ProcessedUsedSize/1GB,2)}},
@{Name="Data Read (GB)"; Expression = {[Math]::Round($_.Info.Progress.ReadSize/1GB,2)}},
@{Name="Transferred (GB)"; Expression = {[Math]::Round($_.Info.Progress.TransferedSize/1GB,2)}},
Result  | ConvertTo-HTML -Fragment
               



$bodySessSuccBk >c:\temp\test.html
ronnmartin61
Veeam Software
Posts: 441
Liked: 131 times
Joined: Mar 07, 2016 3:55 pm
Full Name: Ronn Martin
Contact:

Re: Report Year/month

Post by ronnmartin61 » 1 person likes this post

Note that if you're using v11 or up there is no need to try and add the legacy Veeam PowerShell snapin. We are now leveraging a PowerShell module.

the following should do it. Usage: ...\myscript.ps1 "3/1/23" "3/3/23 23:59:59"

Code: Select all

param
(
[ValidateNotNullorEmpty()][string]$startDate=$(throw "Provide a start date"),
[ValidateNotNullorEmpty()][string]$endDate=$(throw "Provide an end date")
)

$tempArry =@()
$tempArry = Get-VBRBackupSession
$allSesh = $allSesh+ $tempArry 

$tempArry =@()
$tempArry = @(Get-VBRJob | ? {$_.JobType -eq "Backup"})
$allJobsBk = $allJobsBk + $tempArry
# Gather all Backup sessions within timeframe
$seshListBk = @($allSesh | ?{($_.CreationTime -ge $startDate -and $_.CreationTime -le $endDate) -and $_.JobType -eq "Backup"})

# Get Backup session information
$totalxferBk = 0
$totalReadBk = 0
$seshListBk | %{$totalxferBk += $([Math]::Round([Decimal]$_.Progress.TransferedSize/1GB, 2))}
$seshListBk | %{$totalReadBk += $([Math]::Round([Decimal]$_.Progress.ReadSize/1GB, 2))}
$tempSeshListBk = $seshListBk
$seshListBk = @()
Foreach($job in (Get-VBRJob | ? {$_.JobType -eq "Backup"})) {
$seshListBk += $TempSeshListBk | ?{$_.Jobname -eq $job.name} | Sort-Object CreationTime -Descending | Select-Object -First 10
}

$successSessionsBk = @($seshListBk | ?{$_.Result -eq "Success"})
$warningSessionsBk = @($seshListBk | ?{$_.Result -eq "Warning"})
$failsSessionsBk = @($seshListBk | ?{$_.Result -eq "Failed"})
$runningSessionsBk = @($allSesh | ?{$_.State -eq "Working" -and $_.JobType -eq "Backup"})
$failedSessionsBk = @($seshListBk | ?{($_.Result -eq "Failed") -and ($_.WillBeRetried -ne "True")})

$All = @($seshListBk)



$bodySessSuccBk = $All | Sort Name, Creationtime | Select @{Name="Job Name"; Expression = {$_.Name}},
                @{Name="Start Time"; Expression = {$_.CreationTime}},
                @{Name="Stop Time"; Expression = {$_.EndTime}},
@{Name="Duration (Mins)"; Expression = {[Math]::Round($_.WorkDetails.WorkDuration.TotalMinutes,2)}},
@{Name="Avg Speed (MB/s)"; Expression = {[Math]::Round($_.Info.Progress.AvgSpeed/1MB,2)}},
@{Name="Total (GB)"; Expression = {[Math]::Round($_.Info.Progress.ProcessedSize/1GB,2)}},
@{Name="Processed (GB)"; Expression = {[Math]::Round($_.Info.Progress.ProcessedUsedSize/1GB,2)}},
@{Name="Data Read (GB)"; Expression = {[Math]::Round($_.Info.Progress.ReadSize/1GB,2)}},
@{Name="Transferred (GB)"; Expression = {[Math]::Round($_.Info.Progress.TransferedSize/1GB,2)}},
Result  | ConvertTo-HTML -Fragment
               



$bodySessSuccBk >c:\temp\test.html
mateus.kyn
Novice
Posts: 3
Liked: never
Joined: Mar 23, 2023 5:58 pm
Full Name: Mateus Gomes
Contact:

Re: Report Year/month

Post by mateus.kyn »

Dear Ronn

Awesome !!
Thanks for help-me to upgrade the script.
I tested using the form that you send, also this working very well
But i'm afraid to how long time the script will be get informations.
For example if i put a date between a one year
I think we need use Task schedule to running on background.

i was think put this parameters on script, but i'm a little bit confused to put :(
$fromdate = Get-Date -Year 2021 -Month 02 -Day 1 -Hour 0 -Minute 0 -Second 0
$todate = Get-Date -Year 2022 -Month 02 -Day 20 -Hour 0 -Minute 0 -Second 0

I Think like this we can put the day / month / year on script to execute on background.

Also if you have another way to set days on body from script, i think its good to everyone.

Thanks again for help-me

Mateus Gomes
david.domask
Veeam Software
Posts: 1226
Liked: 323 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Report Year/month

Post by david.domask » 2 people like this post

Heya @mateus.kyn,

For the performance time, regrettably the session cmdlets could use a little love here for filtering. If you want a fast and dirty (and unsupported) way of doing this, you can use .NET Reflection.

[Veeam.Backup.Core.CBackupSession]::GetAll()

Save that to a variable and then create new arrays based on the CreationTime property of the Session Objects returned by the command. Note that in v12 it looks like we do some "lazy" loading of the DLLs, so you might need to have your script run an actual cmdlet before running .NET Reflection (something simple like Get-VBRLicenseAutoUpdateStatus and just pipe the ouptut to Out-Null). If you don't do this, you'll get an Object Reference error.

As for:
> Also if you have another way to set days on body from script, i think its good to everyone.

I'm not quite sure I understand the question; you want a way to pass parameters to the script when running it to set the start/end date? If so I can help, but please confirm this part for me just so I'm not misunderstanding.
David Domask | Product Management: Principal Analyst
mateus.kyn
Novice
Posts: 3
Liked: never
Joined: Mar 23, 2023 5:58 pm
Full Name: Mateus Gomes
Contact:

Re: Report Year/month

Post by mateus.kyn »

Hey @david.domask.

Sorry for the delay, i was busy solving a cases :(

Yes u are right about the parameters that i was trying to put.
For example
I wanna see backups that started on 01/03/22
and the last backups on 01/02/23
I believe that its like a anual report on veeam.
I was trying make this, because i didn't saw on console from veeam.

Thanks
Mateus Gomes
david.domask
Veeam Software
Posts: 1226
Liked: 323 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Report Year/month

Post by david.domask » 1 person likes this post

Hi @mateus.kyn,

Got it. This isn't Veeam specific then, you just need to check on setting the parameters and decide how you want to pass it.

https://ss64.com/ps/syntax-args.html

This gives a pretty good example. I think you might want to do something like:

param(
[int]$MonthNumber,
[int]$DayNumber
)

This will let you pass -MonthNumber and -DayNumber to your script. You can then run some code like

$StartDate = Get-Date -Month $MonthNumber -Day $DayNumber
$EndDate = $StartDate.AddMonths(1)

Then filter on the StartDate/EndDate values.

But this is just how I would think about it initially if it were me making a script for myself to fire-off every so often. If you wanted to make it some scheduled tasks that just sends you an email, you'd need to write some logic to find the start/end of the current month based on the current date returned by Get-Date, and then the script can be set in TaskScheduler to run automatically and just use Send-MailMessage to send the report.
David Domask | Product Management: Principal Analyst
Post Reply

Who is online

Users browsing this forum: No registered users and 13 guests