PowerShell script exchange
Post Reply
mrholm
Expert
Posts: 170
Liked: 15 times
Joined: Apr 20, 2018 8:12 am
Full Name: Mats Holm
Contact:

A script to report on specific servers within a job?

Post by mrholm »

Hi
I'm not a Powershell guy so that's why ask you all.
One of our job backups 250 servers and I have on application owner that wants to have daily mail reports on "his" servers within the job.
Do anyone have a script that can create a backup report from a backup job on succes or not, start/end time and so on but only on specific servers within the job?

Happy for anything
//Mats
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: A script to report on specific servers within a job?

Post by oleg.feoktistov »

Hi Mats,

Don't have such a script at hand but let's see what I can help you with.
What is the condition that defines your application owner's servers within the job?

Thank you,
Oleg
mrholm
Expert
Posts: 170
Liked: 15 times
Joined: Apr 20, 2018 8:12 am
Full Name: Mats Holm
Contact:

Re: A script to report on specific servers within a job?

Post by mrholm »

Hi
Since there's around 10 servers I think a seprate "input" file with server names could work as selection.
Egor Yakovlev
Veeam Software
Posts: 2536
Liked: 680 times
Joined: Jun 14, 2013 9:30 am
Full Name: Egor Yakovlev
Location: Prague, Czech Republic
Contact:

Re: A script to report on specific servers within a job?

Post by Egor Yakovlev »

Hi Mats!
Aside of PowerShell your goal can be easily achieved via Veeam One user-friendly UI. It takes 15 minutes to setup an Application Owner business unit and schedule something like Daily VM Protection Status Report or VM Protection History. Both Business Unit and Reports are dynamic by nature, so if your app owner creates new VM, it is automatically discovered and added to his scheduled protection reports!
/Cheers!
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: A script to report on specific servers within a job?

Post by oleg.feoktistov »

Hi Mats,

Here is a sample script for your case:

Code: Select all

asnp VeeamPSSnapin
$smtpName= 'smtpServer'
$to = '$recipient@domain.com'
$from = 'sender@domain.com'
$subject = 'Backup Report'
$date = (Get-Date).ToShortDateString()
$vms = Get-Content -Path 'C:\Temp\vms.csv'
$sessions = Read-Host -Prompt 'Enter Backup / Backup Copy job name followed by * '
$objectsReport = @()
foreach ($session in $sessions) {
    $session = Get-VBRBackupSession -Name $session
    $sessionDate = ($session.CreationTimeUTC).ToShortDateString()
    if ($sessionDate -eq $date) { 
    $taskSessions = Get-VBRTaskSession -Session $session
    foreach ($vm in $vms) {
        foreach ($taskSession in $taskSessions) {
            if ($vm -eq $taskSession.Name) {
                $objectsReport += $taskSession  | select Name, JobName, Status, @{n='Start Time';e={$_.Progress.StartTimeLocal}}, @{n='End Time';e={$_.Progress.StopTimeLocal}}
        }
     }
   }
 }
}
$body = $objectsReport | ConvertTo-Html
$message = [System.Net.Mail.MailMessage]::new($from, $to, $subject, $body)
$message.IsBodyHtml = $True
$smtp = [System.Net.Mail.SmtpClient]::new($smtpName)
$smtp.Send($message) 
It takes a list of VM names saved in C:\Temp\vms.csv and sends simple backup report for these VMs with today's statistics only.
In this script you need to specify backup job name explicitly in the script body to get it work.

Thanks,
Oleg
mrholm
Expert
Posts: 170
Liked: 15 times
Joined: Apr 20, 2018 8:12 am
Full Name: Mats Holm
Contact:

Re: A script to report on specific servers within a job?

Post by mrholm »

Hi Oleg
Thanks, I have created the script and input file but can't seem to get any data out of it.
I have edited thesections on smtp (mail is sent nicely) and also on the backup job
One thing I wonder is the actual object insertion as below, do I have tp set Start Time and End Time?

$objectsReport += $taskSession | select Name, JobName, Status, @{n='Start Time';e={$_.Progress.StartTimeLocal}}, @{n='End Time';e={$_.Progress.StopTimeLocal}}
mrholm
Expert
Posts: 170
Liked: 15 times
Joined: Apr 20, 2018 8:12 am
Full Name: Mats Holm
Contact:

Re: A script to report on specific servers within a job?

Post by mrholm »

Hi again, a litte to fast befpre sending, I see that the start time and end time is what the report is showing, but it seems that I don't get any Backup Session back with the Get-VBRBackupSession, cause if I run only that selection I have the $sessions empty
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: A script to report on specific servers within a job?

Post by oleg.feoktistov »

Hi Mats,

In my script $sessions variable held the output of Get-VBRBackupSession -Name 'Backup*' as an example.
For your convenience I changed it to receive input data from the console. Now it will prompt you for backup job name and query all the backup sessions for the current day based on the name inputted. Make sure to input job name followed by a wildcard so that the script could query both Full and Incremental backup sessions.

Have a try and see if it helps.

Thanks,
Oleg
mrholm
Expert
Posts: 170
Liked: 15 times
Joined: Apr 20, 2018 8:12 am
Full Name: Mats Holm
Contact:

Re: A script to report on specific servers within a job?

Post by mrholm »

Hi Oleg
Still just an empty report. As foe now when i send in a name and followed by a * it will give me a lot of jobs since our jobs have similair names so I like to have only one job to look for.
The following is what I have in the script:

Code: Select all

asnp VeeamPSSnapin
$vbrServer = "****"
$smtpName= '****'
$to = '****m'
$from = '****'
$subject = 'Backup Report'
$date = (Get-Date).ToShortDateString()
$vms = Get-Content -Path 'D:\Veeam_Scripts\vm-report-in-job.txt'
#$sessions = Get-VBRBackupSession -Name 'swe006_backup_vm_std_prod'
$sessions = Read-Host -Prompt 'Enter Backup job name followed by * '
$objectsReport = @()
foreach ($session in $sessions) {
    $session = Get-VBRBackupSession -Name $session
    $sessionDate = ($session.CreationTimeUTC).ToShortDateString()
    if ($sessionDate -eq $date) { 
    $taskSessions = Get-VBRTaskSession -Session $session
    foreach ($vm in $vms) {
        foreach ($taskSession in $taskSessions) {
            if ($vm -eq $taskSession.Name) {
                $objectsReport += $taskSession  | select Name, JobName, Status, @{n='Start Time';e={$_.Progress.StartTimeLocal}}, @{n='End Time';e={$_.Progress.StopTimeLocal}}
        }
     }
   }
 }
}
$body = $objectsReport | ConvertTo-Html
$message = [System.Net.Mail.MailMessage]::new($from, $to, $subject, $body)
$message.IsBodyHtml = $True
$smtp = [System.Net.Mail.SmtpClient]::new($smtpName)
$smtp.Send($message)
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: A script to report on specific servers within a job?

Post by oleg.feoktistov »

You need to pass to the prompt as many unique characters of a job name as possible to make it work. ' * ' is a necessity as all job session names consist of job name and (full)/(incremental) addition.
So, for instance, if you have similar job names like 'swe006_backup_vm_std_prod' and 'swe006_backup_vm_std_prod-02', to query for statistics related to the first job only, you can specify swe006_backup_vm_std_prod * in job name prompt.
Cheers!
wane
Enthusiast
Posts: 39
Liked: 5 times
Joined: Jun 26, 2020 9:47 pm
Full Name: Walter Neu
Contact:

Re: A script to report on specific servers within a job?

Post by wane »

Hi Oleg,

I don't know what I'm doing wrong. I always got

Code: Select all

+     $sessionDate = ($session.CreationTimeUTC).ToShortDateString()
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
What I need is something like such an output, but only for a certain VM as discussed in this thread, which is specified in CSV oder text-file.

Code: Select all

Name            : pcli.edsb.eurodata.de
StartTimeLocal  : 07.06.2021 17:30:46
StopTimeLocal   : 07.06.2021 17:33:02
Duration        : 00:02:16.7800000
The script above gives always the error concerning the time.
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: A script to report on specific servers within a job?

Post by oleg.feoktistov »

Hi Walter,

The error implies that you have tried to call the method on a null value meaning that $session variable is empty.
Try checking the output of $session and make sure that session object is actually written there.
If this is not the issue, please, share the whole script so that we could assist you further.

Thanks,
Oleg
wane
Enthusiast
Posts: 39
Liked: 5 times
Joined: Jun 26, 2020 9:47 pm
Full Name: Walter Neu
Contact:

Re: A script to report on specific servers within a job?

Post by wane »

It is exactly the same script from this thread, I have not changed anything (except the necessary parameters). But maybe this is just a workaround.

I need a way to pass a list of VMs (e.g. c:\vm.txt) that are backed up in different backup jobs and get the exact start and end time of their backup as output.
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: A script to report on specific servers within a job?

Post by oleg.feoktistov »

Hi Walter,

Apologies, it was a mistake in my script - I didn't pass session name correctly. The logic was also a bit off. I adjusted it accordingly.
Please try this sample:

Code: Select all

 $path = Read-Host -Prompt 'Enter full path to a vms list'
$vms = Get-Content -Path $path
$jobs = Get-VBRJob
$allSessions = Get-VBRBackupSession
$objectsReport = @()
foreach ($job in $jobs) {
    $objects = Get-VBRJobObject -Job $job -Name $vms
    if ($objects) {
    $sessions = $allSessions | where {$_.JobId -eq $job.Id}
    foreach ($object in $objects) {
     foreach ($session in $sessions) {
        $taskSession = Get-VBRTaskSession -Session $session -Name $object.Name
        $objectsReport += $taskSession | select Name, `
        @{n='StartTimeLocal'; e={$_.Progress.StartTimeLocal.ToString()}},
        @{n='StopTimeLocal';e={$_.Progress.StopTimeLocal.ToString()}}, `
        @{n='Duration';e={$_.Progress.Duration}} | fl
     }   
   }
 }
}
$objectsReport 
Now, since we query job entities here, this script is not applicable for Exported and VeeamZIP backups.
Also, the official and supported way to retrieve all the sessions is used here. If it comes to retrieving numerous sessions, might impact script's performance as Get-VBRBackupSession obtains all backup sessions from Veeam DB first, even if we pass precise name to it.
To get sessions faster, you can use .NET method instead to get sessions by job id, which is unsupported but does the job. If you consider adding it, insert it after $objects variable is validated:

Code: Select all

#$allSessions = Get-VBRBackupSession
...
if ($objects) {
    $sessions = [Veeam.Backup.Core.CBackupSession]::GetByJob($job.Id)
...
Best regards,
Oleg
wane
Enthusiast
Posts: 39
Liked: 5 times
Joined: Jun 26, 2020 9:47 pm
Full Name: Walter Neu
Contact:

Re: A script to report on specific servers within a job?

Post by wane »

Hi Oleg,

in Tina Turner's words: you're simply the best.

I tuned your script a little bit and it works fine so far. Only the format of the email is not working.
When I pipe the output to ConvertTo-HTML for the email body, it looks like

Code: Select all

ClassId2e4f51ef21dd47e99d3c952918aff9cd	pageHeaderEntry	pageFooterEntry	autosizeInfo	shapeInfo	groupingEntry
033ecb2bc07a4d43b5ef94ed5a35d280				Microsoft.PowerShell.Commands.Internal.Format.ListViewHeaderInfo	
9e210fe47d09416682b841769c78b8a3					
27c87ef9bbda4f709f6b4002fa4af63c					
4ec4f0187cb04f4cb6973460dfe252df					
cf522b78d86c486691226b40aa69e95c					
Whne I pipe to Out-String the result is:

Code: Select all

Name : t-win.eurodata.de Start : 10.06.2021 05:00:38 Stop : 10.06.2021 05:02:10 Duration : 00:01:32.7270000 
that meeans the output in one line...

It's frustrating....
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: A script to report on specific servers within a job?

Post by oleg.feoktistov »

Hi Walter,

It appears that ConvertTo-Html doesn't serialize custom objects, which were formatted with Format-List cmdlet, correctly.
I suggest appending custom object inside sessions loop without piping it to "fl". Then it works correctly.

Thanks,
Oleg
Post Reply

Who is online

Users browsing this forum: No registered users and 19 guests