PowerShell script exchange
jcop
Influencer
Posts: 22
Liked: 2 times
Joined: Jan 29, 2021 2:02 pm
Full Name: Julio Olalla
Contact:

Re: PowerShell to gather backup sizes per server??

Post by jcop »

Thanks for the reply Oleg.

Yes, I need the Total data size and Backup size in a specific month. Since in the report that they are requesting me, I must enter these two data month by month. Example, now I need Total data size and Backup size for January, then in March I will need total data size and backup size for February and so on
wishr
Veteran
Posts: 3077
Liked: 453 times
Joined: Aug 07, 2018 3:11 pm
Full Name: Fedor Maslov
Contact:

Re: PowerShell to gather backup sizes per server??

Post by wishr »

Hi Julio,

Could you please take a look at Oleg's post one more time because your post does not answer his question? Should the existence of the backup files on the repository at the time when you run the script be taken into account or not? What has been created and what has been created and remained on the repo are different things.

Thanks in advance.
jcop
Influencer
Posts: 22
Liked: 2 times
Joined: Jan 29, 2021 2:02 pm
Full Name: Julio Olalla
Contact:

Re: PowerShell to gather backup sizes per server??

Post by jcop »

Thanks and sorry if I didn't know how to explain.

Yes, you must take into account what has been created in the repository during that month to consult. Previously in this job that I am now there was another person and he did the monthly reports and as I see the reports made by him in the months of November-December the BackupSize and Datasize data are data in TB. For example, the November datasize was 72.14 TB and the November Backupsize was: 10.5 TB. The data size for December was 69.7 TB and the Backup Size for December was: 9.2 TB
No backup jobs have been deleted, there is the same amount so I guess the data for January should be similar
jcop
Influencer
Posts: 22
Liked: 2 times
Joined: Jan 29, 2021 2:02 pm
Full Name: Julio Olalla
Contact:

Re: PowerShell to gather backup sizes per server??

Post by jcop »

Excuse me, I don't know if I knew how to explain this time in the post before this one. That is why I think that the script that you sent me and that allows me to select a date range works perfect but when I select a month, for example from January 1 to January 31, the result that it gives me are:
datasize: 800GB and backupsize: 497GB. And according to reports from previous months of November and December, they should be larger, that is, in TB

Excuse me, I hope I can explain this so that you can help me. Thanks a lot
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: PowerShell to gather backup sizes per server??

Post by oleg.feoktistov »

A quick question while I'm changing the script. What happened to the methods the other guy used to compose this report? :)
jcop
Influencer
Posts: 22
Liked: 2 times
Joined: Jan 29, 2021 2:02 pm
Full Name: Julio Olalla
Contact:

Re: PowerShell to gather backup sizes per server??

Post by jcop »

Hi Oleg! thanks for responding and for your help.

He left a script that I can share with you here. We put the date parameter for example 2020-12-01 2020-12-31 and it gives us the Backupsize data and data size of each machine in all the days of that month of December but when adding them all gives us a super high result that does not correspond to what he put in the reports. I do not know if he used another script apart and if so, he did not leave it within our reach.

The script is this:

Code: Select all

param([datetime]$fechainicial,[datetime]$fechafinal)
#yyyy-MM-dd (inicial) yyyy-MM-dd (final)
<#
$Title = "informacion de backup session"
$Comment ="Obtiene informacion de las sesiones de los backup jobs: 
nombre
tipo
hora de comienzo
hora de termino
duracion
resultado
                    
Muestra en consola el resultado y exporta en csv."
Para mostrar en consola descomentar write-host de $alldetails
$Author = "Deborah del Solar"
$Date = "Febrero 2018"
$Version = "v1.0"
#>

if ((Get-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue) -eq $null) {
    Add-PsSnapin -Name VeeamPSSnapIn
}


$jobs = Get-VBRBackupSession
$allDetails = @() 

foreach ($job in $jobs | where { $_.JobType -eq "Backup"}) 

    {
    
    $fecha = (Get-date $job.CreationTime -f "dd-MM-yyyy")
    
    if ((Get-Date $fecha) -ge (Get-Date $fechainicial) -and
            (Get-Date $fecha) -le (Get-Date $fechafinal))
        {       
     
   $split = $job.name.Split("( )") 

   $jobOptions = New-Object PSObject
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Name" -value $job.Info.JobName
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Tipo" -value $split[2] 
   $jobOptions | Add-Member -MemberType NoteProperty -Name "StartTime" -value $job.Info.CreationTime
   $jobOptions | Add-Member -MemberType NoteProperty -Name "EndTime" -value $job.Info.EndTime
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Durationhora" -value ($job.Info.Progress.Duration.Hours.ToString()+":"+$job.Info.Progress.Duration.Minutes.ToString()+":"+$job.Info.Progress.Duration.Seconds.ToString())
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Resultado" -value $job.Info.Result
   $jobOptions | Add-Member -MemberType NoteProperty -Name "BackupSize" -value ('{0:0.00}' -f ($job.BackupStats.BackupSize/1GB))
   $jobOptions | Add-Member -MemberType NoteProperty -Name "DataSize" -value ($job.BackupStats.DataSize/1GB)
 
   
   $allDetails += $jobOptions
   
        }
    }

   #Write-Host $allDetails
   $allDetails | Export-Csv -NoTypeInformation C:\Users\adm\Desktop\Reportes\session.csv 

   

$tapeJobs = Get-VBRTapeJob
$tapeAllDetails = @() 

foreach ($tapeJob in $tapeJobs) 

    {

    $tapeSessions = Get-VBRSession -Job $tapeJob

    foreach ($tapeSession in $tapeSessions)

    {

    
    $fecha = (Get-date $tapeSession.CreationTime -f "dd-MM-yyyy")
    
    if ((Get-Date $fecha) -ge (Get-Date $fechainicial) -and
            (Get-Date $fecha) -le (Get-Date $fechafinal))
        {       
     
                    $tapeJobOptions = New-Object PSObject
           $tapeJobOptions | Add-Member -MemberType NoteProperty -Name "Name" -value $tapeJob.Name
           $tapeJobOptions | Add-Member -MemberType NoteProperty -Name "Tipo" -value $tapeJob.Type
           $tapeJobOptions | Add-Member -MemberType NoteProperty -Name "StartTime" -value $tapeSession.CreationTime
           $tapeJobOptions | Add-Member -MemberType NoteProperty -Name "EndTime" -value $tapeSession.EndTime
           $tapeJobOptions | Add-Member -MemberType NoteProperty -Name "Resultado" -value $tapeSession.Result
           $tapeJobOptions | Add-Member -MemberType NoteProperty -Name "Progreso" -value $tapeSession.Progress
 
   
           $tapeAllDetails += $tapeJobOptions
        }
   
        }
    }

   #Write-Host $allDetails
   $tapeAllDetails | Export-Csv -NoTypeInformation C:\Users\adm\Desktop\Reportes\tapeSession.csv
 
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: PowerShell to gather backup sizes per server??

Post by oleg.feoktistov »

Hi Julio,

This script looks much alike with what I changed mine to. Here it is:

Code: Select all

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object Windows.Forms.Form -Property @{
    StartPosition = [Windows.Forms.FormStartPosition]::CenterScreen
    Size          = New-Object Drawing.Size 243, 230
    Text          = 'Select a Date'
    Topmost       = $true

}

$form.AutoSize = $true
$form.AutoSizeMode = 'GrowAndShrink'
$form.FormBorderStyle = 'FixedDialog'
$form.KeyPreview = $true
$form.MaximizeBox = $false
$form.Text = 'Calendar'
$form.add_Shown({$form.Activate()})
$form.add_KeyDown({
    switch($_.KeyCode) {
        'Escape' {$form.Close()}
        'Return' {$form.Close(); $Start = $cal.SelectionRange.Start; $End = $cal.SelectionRange.End}
    }
})

$calendar = New-Object Windows.Forms.MonthCalendar -Property @{
    ShowTodayCircle   = $false
    MaxSelectionCount = 366
}
$form.Controls.Add($calendar)

$okButton = New-Object Windows.Forms.Button -Property @{
    Location     = New-Object Drawing.Point 38, 165
    Size         = New-Object Drawing.Size 75, 23
    Text         = 'OK'
    DialogResult = [Windows.Forms.DialogResult]::OK
}
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)

$cancelButton = New-Object Windows.Forms.Button -Property @{
    Location     = New-Object Drawing.Point 113, 165
    Size         = New-Object Drawing.Size 75, 23
    Text         = 'Cancel'
    DialogResult = [Windows.Forms.DialogResult]::Cancel
}
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)

$result = $form.ShowDialog()

if ($result -eq [Windows.Forms.DialogResult]::OK) {
    $dateStart = $calendar.SelectionStart
    $dateEnd = $calendar.SelectionEnd
    Write-Host "Range selected: $($dateStart.ToShortDateString()) - $($dateEnd.ToShortDateString())"
}



# Construct a new hastable for monthly report
$reportMonth = @{
  dataSizeGB = $report.dataSizeGB
  backupSizeGB = $report.backupSizeGB

}
$sessions = Get-VBRBackupSession | where {$_.CreationTime -ge $dateStart -and $_.CreationTime -le $dateEnd}
# Get all storages created for the last month
foreach ($session in $sessions) {
  
    $reportMonth.dataSizeGB = $reportMonth.dataSizeGB + [Math]::Round($session.BackupStats.DataSize/1GB, 2)
    $reportMonth.backupSizeGB = $reportMonth.backupSizeGB + [Math]::Round($session.BackupStats.BackupSize/1GB, 2)
  
} 


$reportMonth = $reportMonth | select @{n='dataSizeGB';e={$_.dataSizeGB}}, @{n='backupSizeGB';e={$_.backupSizeGB}}, @{n='DateRangeStart';e={$dateStart.ToShortDateString()}}, @{n='DateRangeEnd';e={$dateEnd.ToShortDateString()}}

$reportMonth # | Export-Csv -Path 'C:\report.csv' -NoTypeInformation 




If we are talking about backup and data size processed in a particular month, we mean sessions. Though the problem here is that the cmdlet we are after displays only sessions of certain type, whereas the cmdlets for other sessions types (like tape backup sessions) contain no info on backup and data sizes.
Moreover, in the script you shared, instead of requesting just all sessions, they are filtered by backup type. So, backup copy and replica sessions are all ignored.
That might be why such discrepancy between the data you see running the script and the data put in the report.

Thanks,
Oleg
jcop
Influencer
Posts: 22
Liked: 2 times
Joined: Jan 29, 2021 2:02 pm
Full Name: Julio Olalla
Contact:

Re: PowerShell to gather backup sizes per server??

Post by jcop »

Hi Oleg,

Sorry, I don't understand why, but with this last script that you send me, it gives me a super high GB number, for example I did the search from January 1 to January 31, 2021 and it gave me the following:
Datasize: 420224 GB (410.38 TB)
Backupsize: 125,913 GB (122.96 TB)
It's a super high number. According to reports from previous months such as November 2020 December 2020 the Datasize was between 65 and 70 TB and the Backup Size between 9 and 10 TB
And the number of jobs has not changed, they remain the same. So I imagine that those numbers of Backupsize and Data size for example of January 2021 should be very similar, or the variation should be very little. But as you can see, the size of Datasize and Backupsize of January 2021 is super large compared to November or December 2020
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: PowerShell to gather backup sizes per server??

Post by oleg.feoktistov »

Hi Julio,

To be honest, I don't quite understand what causes such discrepancy between reports. The script works fine in my environment and the data being reflected is corresponds to the amount I backup. I'm almost out of ideas, but here is a dumb one: failed sessions also report backup and data sizes, so maybe a bunch of them is causing this. It's unlikely to be true as the gap between data size reported is too high, but worth trying, I think.
In the script please change this:

Code: Select all

$sessions = Get-VBRBackupSession | where {$_.CreationTime -ge $dateStart -and $_.CreationTime -le $dateEnd}
To this:

Code: Select all

$sessions = Get-VBRBackupSession | where {$_.CreationTime -ge $dateStart -and $_.CreationTime -le $dateEnd -and $_.Result -ne "Failed"}
Otherwise, it'd be better to have the script the other guy used to see why it is happening.

Thanks,
Oleg
jcop
Influencer
Posts: 22
Liked: 2 times
Joined: Jan 29, 2021 2:02 pm
Full Name: Julio Olalla
Contact:

Re: PowerShell to gather backup sizes per server??

Post by jcop »

Hi Oleg, I really appreciate all your help. I tried the change you made in the script but it still shows me super large results from the datasize and backupsize of January 2021.
Data size: 407 589 GB
Backupsize: 121 341 GB

Regarding the script used by the person who no longer works here, I'll leave it here for you. In the same way I tell you to use it and it shows me the datasize and backupsize machine by machine in the month you select but if I add them all the same gives me a super large number. That's why I started my search because I don't understand where he got those numbers from.

Here is the script left by the person who no longer works here:

Code: Select all

param([datetime]$fechainicial,[datetime]$fechafinal)
#yyyy-MM-dd (inicial) yyyy-MM-dd (final)
<#
$Title = "informacion de backup session"
$Comment ="Obtiene informacion de las sesiones de los backup jobs: 
nombre
tipo
hora de comienzo
hora de termino
duracion
resultado
                    
Muestra en consola el resultado y exporta en csv."
Para mostrar en consola descomentar write-host de $alldetails
$Author = "Deborah del Solar"
$Date = "Febrero 2018"
$Version = "v1.0"
#>

if ((Get-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue) -eq $null) {
    Add-PsSnapin -Name VeeamPSSnapIn
}


$jobs = Get-VBRBackupSession
$allDetails = @() 

foreach ($job in $jobs | where { $_.JobType -eq "Backup"}) 

    {
    
    $fecha = (Get-date $job.CreationTime -f "dd-MM-yyyy")
    
    if ((Get-Date $fecha) -ge (Get-Date $fechainicial) -and
            (Get-Date $fecha) -le (Get-Date $fechafinal))
        {       
     
   $split = $job.name.Split("( )") 

   $jobOptions = New-Object PSObject
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Name" -value $job.Info.JobName
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Tipo" -value $split[2] 
   $jobOptions | Add-Member -MemberType NoteProperty -Name "StartTime" -value $job.Info.CreationTime
   $jobOptions | Add-Member -MemberType NoteProperty -Name "EndTime" -value $job.Info.EndTime
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Durationhora" -value ($job.Info.Progress.Duration.Hours.ToString()+":"+$job.Info.Progress.Duration.Minutes.ToString()+":"+$job.Info.Progress.Duration.Seconds.ToString())
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Resultado" -value $job.Info.Result
   $jobOptions | Add-Member -MemberType NoteProperty -Name "BackupSize" -value ('{0:0.00}' -f ($job.BackupStats.BackupSize/1GB))
   $jobOptions | Add-Member -MemberType NoteProperty -Name "DataSize" -value ($job.BackupStats.DataSize/1GB)
 
   
   $allDetails += $jobOptions
   
        }
    }

   #Write-Host $allDetails
   $allDetails | Export-Csv -NoTypeInformation C:\Users\adm\Desktop\Reportes\session.csv 

   

$tapeJobs = Get-VBRTapeJob
$tapeAllDetails = @() 

foreach ($tapeJob in $tapeJobs) 

    {

    $tapeSessions = Get-VBRSession -Job $tapeJob

    foreach ($tapeSession in $tapeSessions)

    {

    
    $fecha = (Get-date $tapeSession.CreationTime -f "dd-MM-yyyy")
    
    if ((Get-Date $fecha) -ge (Get-Date $fechainicial) -and
            (Get-Date $fecha) -le (Get-Date $fechafinal))
        {       
     
                    $tapeJobOptions = New-Object PSObject
           $tapeJobOptions | Add-Member -MemberType NoteProperty -Name "Name" -value $tapeJob.Name
           $tapeJobOptions | Add-Member -MemberType NoteProperty -Name "Tipo" -value $tapeJob.Type
           $tapeJobOptions | Add-Member -MemberType NoteProperty -Name "StartTime" -value $tapeSession.CreationTime
           $tapeJobOptions | Add-Member -MemberType NoteProperty -Name "EndTime" -value $tapeSession.EndTime
           $tapeJobOptions | Add-Member -MemberType NoteProperty -Name "Resultado" -value $tapeSession.Result
           $tapeJobOptions | Add-Member -MemberType NoteProperty -Name "Progreso" -value $tapeSession.Progress
 
   
           $tapeAllDetails += $tapeJobOptions
        }
   
        }
    }

   #Write-Host $allDetails
   $tapeAllDetails | Export-Csv -NoTypeInformation C:\Users\adm\Desktop\Reportes\tapeSession.csv
jcop
Influencer
Posts: 22
Liked: 2 times
Joined: Jan 29, 2021 2:02 pm
Full Name: Julio Olalla
Contact:

Re: PowerShell to gather backup sizes per server??

Post by jcop »

Hello Oleg,
I was reviewing one of the scripts you sent me a few weeks ago. This script that you sent me told me that it generated the datasize and backupsize report for the previous month. I have run it and the truth is that the result it gives me is a little more similar to those shown in the reports. For example, I ran the script today January 23rd and it displayed the following:

Data Size: 31987 GB
BackupSize: 7765 GB

Let's say they are numbers more similar to those of the reports. But I have a question. If I run this script today, February 23, does it show me the reports for the entire month of January? That is, that result that it shows me is from January 1 to January 31?

The Script I'm talking about that you sent me a few weeks ago is the following:

Code: Select all

# Get all backups and current date
asnp VeeamPSSnapin
$ backups = Get-VBRBackup
$ date = Get-Date

# Construct a new hastable for monthly report
$ reportMonth = @ {
  dataSizeGB = $ report.dataSizeGB
  backupSizeGB = $ report.backupSizeGB

}

# Get all storages created for the last month
foreach ($ backup in $ backups) {
  $ storages = $ backup.GetAllStorages () | where {$ _. CreationTime -ge $ date.AddMonths (-1)}
  foreach ($ storage in $ storages) {
    $ reportMonth.dataSizeGB + = [Math] :: Round ($ storage.Stats.DataSize / 1GB, 2)
    $ reportMonth.backupSizeGB + = [Math] :: Round ($ storage.Stats.BackupSize / 1GB, 2)
  }
}

$ reportMonth = $ reportMonth | select @ {n = 'dataSizeGB'; e = {$ _. dataSizeGB}}, @ {n = 'backupSizeGB'; e = {$ _. backupSizeGB}}

$ reportMonth | Export-Csv -Path 'C: \ report.csv' -NoTypeInformation
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: PowerShell to gather backup sizes per server??

Post by oleg.feoktistov »

Hi Julio,

As per the script you shared, I figure that your ex-colleague used the latter approach I talked about.
Why he put different data to the reports remains unknown to me though. The script works perfectly on distinct labs, and the data it reflects looks accurate in terms of labs sizes.

About my first script - it tells how much is the total size of backups, which were created a month ago and still remain on repositories.
If you run the script now, it will reflect total size of the backups produced in January and not deleted by retention as of now.
The approach here is different and is more about auditing existing backups/space left, not reviewing sessions history.
But suppose the numbers are similar to those the first script produces, I venture a guess that this is the way the data was obtained initially
considering that some time passed between the day he ran the script and the day you did. Numerous restore points could have been deleted by retention, a number of them could have been created as well.

Thanks,
Oleg
jcop
Influencer
Posts: 22
Liked: 2 times
Joined: Jan 29, 2021 2:02 pm
Full Name: Julio Olalla
Contact:

Re: PowerShell to gather backup sizes per server??

Post by jcop »

Hello Oleg, thanks again for your great help

Now ask, if I run the old script that you sent me the one that brings the report of the previous month vs the last script that you sent me and in the calendar I select from January 1 to January 31. Should this give me the same result in both scripts?

I imagine not, because I did it but it is not like that. The script that makes the report from the previous month gives me a more credible number and the script where I select the month of January shows me a super large result that is nothing like the one shown by the script from the previous month.

I suppose I should use the script from the previous month because I really don't understand where the person who previously worked here got the numbers from
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: PowerShell to gather backup sizes per server??

Post by oleg.feoktistov »

Hi Julio,

Again, these scripts have absolutely different purpose. For one, it's like reviewing which groceries in your fridge have best before date in the future. For other - analyzing your checks to see how much groceries you bought for the last month or any other date range.

I have a suggestion. Try this report. It mimics job report in the VBR UI. Then calculate backup size for a day for all jobs shown there and compare the result with the one shown with the second script I sent. Make sure to choose the same day in the calendar you calculated backup size for in the mimic report I just shared. This is just to illustrate what I mean. Because, in the end, we might just be talking about different things, and your report requirements are, indeed, to audit backup size of all existing backups.

Best regards,
Oleg
MEKVII
Lurker
Posts: 2
Liked: never
Joined: Mar 13, 2023 2:06 pm
Full Name: Thor
Contact:

Re: PowerShell to gather backup sizes per server??

Post by MEKVII »

The follow script worked for all backups and their sizes. However is there a way to set it to only report for Jobs contained inside of Backups "Disk Imported," rather than every single back up job in veeam?

THE SCRIPT:
$backups = Get-VBRBackup
$output = @()
foreach ($backup in $backups) {
$storages = $backup.GetAllStorages()
foreach ($storage in $storages) {
$output += $storage | select @{n='Name';e={$_.PartialPath}}, @{n='DataSize';e={$_.Stats.DataSize}}, `
@{n='BackupSize';e={$_.Stats.BackupSize}}
}
}
$output | Export-Csv -Path 'C:\Users\Desktop\output.csv' -NoTypeInformation
david.domask
VeeaMVP
Posts: 1033
Liked: 278 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: PowerShell to gather backup sizes per server??

Post by david.domask »

Heya @MEKVII,

CBackup objects have an IsImported property you can filter on. Should be boolean.
David Domask | Director: Customer Care | Veeam Technical Support
Post Reply

Who is online

Users browsing this forum: No registered users and 17 guests