PowerShell script exchange
Post Reply
jbyoon
Novice
Posts: 6
Liked: never
Joined: Feb 06, 2020 12:07 pm
Full Name: YOON JOO BYOUNG
Contact:

Question - Backup Job check powershell for Last 24 hours

Post by jbyoon »

1. I want to check the status (success/failure) of the backup job executed within 24 hours of the current time with PowerShell.

2. In the case of VMware backup, you could check with the PowerShell below. (Check the backup status for the last 10 days for the VM)

Get-VBRBackupSession | ?{$_.CreationTime -ge (Get-Date).AddDays(-10)} | Select JobName, JobType, CreationTime, Result, @{Name="BackupSize";Expression={$_.BackupStats.BackupSize}} | Sort CreationTime | Format-Table
Image

3. By the way, the backup job of Agent (Windows and Linux) is not output with the above PowerShell. and I would like to display the status of the Agent job as in PowerShell for VMware Backup.

4. How can I check the status (success/failure) of VM and physical machine full backup jobs?
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Question - Backup Job check powershell for Last 24 hours

Post by oleg.feoktistov »

Hi,

So, let me recap. The main points are two:

1. Get all backup job sessions ran for the last 24 hours. Datetime class has another method for that. Since you already use AddDays(), I guess you can easily figure out how it's called for hours.

2. Get agent backup job sessions. Use Get-VBRComputerBackupJobSessions cmdlet for that.

Thanks,
Oleg
jbyoon
Novice
Posts: 6
Liked: never
Joined: Feb 06, 2020 12:07 pm
Full Name: YOON JOO BYOUNG
Contact:

Re: Question - Backup Job check powershell for Last 24 hours

Post by jbyoon »

Thanks, oleg.feoktistov !!

Thanks to you, I made a PowerShell to check the backup status of the physical server.

Code: Select all

foreach ($PBackup_job in (Get-VBRComputerBackupJob | Select Name)) {
	$PBackup_job_name = $PBackup_job.Name
	Get-VBRComputerBackupJobSession -Name $PBackup_job_name | Where-Object { $_.CreationTime -gt (Get-Date).AddDays(-1)} | Sort CreationTime | Format-Table  
	}
But, in the
$_.CreationTime -gt (Get-Date).AddDays(-1)

part, the result value one day before I want is not displayed, but the contents of the entire date are displayed.


This option works without any problems in below..

Code: Select all

Get-VBRBackupSession | ?{$_.CreationTime -ge (Get-Date).AddDays(-1)} | Select JobName, JobType, CreationTime, endtime, result, state, @{Name="BackupSize";Expression={$_.BackupStats.BackupSize}} | Sort CreationTime | Format-Table


OutPut:
JobName JobType CreationTime         EndTime               Result   State  BackupSize
------- ------- ------------         -------               ------   -----  ----------
VM_Job   Backup 3/21/2021 8:17:49 AM 3/21/2021 8:41:59 AM Warning Stopped 18063425536

Anyone ask help me.
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Question - Backup Job check powershell for Last 24 hours

Post by oleg.feoktistov »

Hi,
the result value one day before I want is not displayed, but the contents of the entire date are displayed.
Sorry, I'm not sure what you mean by that.

Try using AddHours() method on Get-Date, looks like it fits more in your case.

Thanks,
Oleg
jbyoon
Novice
Posts: 6
Liked: never
Joined: Feb 06, 2020 12:07 pm
Full Name: YOON JOO BYOUNG
Contact:

Re: Question - Backup Job check powershell for Last 24 hours

Post by jbyoon »

thank you for the reply.

I've tried running 2 powershells to check the status of backup jobs that have been running for 24 hours from the current time.

1. Where-Object { $_.CreationTime -ge (Get-Date).AddDays(-1)}
Image

2. Where-Object { $_.CreationTime -ge (Get-Date).AddHours(-1)}
- AddHours(-1) ~ AddHours(-4) = Not OutPut
- AddHours(-5) ~ AddHours(-6) ..... = All DATA OutPut

Image

I only want to outputs a list of backup jobs within 24 hours. By the way, the above PowerShell command outputs a list of backup jobs for all periods.
Which command should I apply to get the result I want?
morciodm
Novice
Posts: 9
Liked: 1 time
Joined: Sep 04, 2018 3:03 pm
Full Name: debra morcio
Contact:

Re: Question - Backup Job check powershell for Last 24 hours

Post by morciodm »

Are there fields specific to NAS File Share backups? these are not being reflected in my Veeam backup daily report? its only VM's
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Question - Backup Job check powershell for Last 24 hours

Post by oleg.feoktistov » 1 person likes this post

@jbyoon,

For some reason, the output of Get-VBRComputerBackupJobSession cannot be filtered and sorted directly.
Try the approach below:

Code: Select all

$date = (Get-Date).AddHours(-24)
$sessions = Get-VBRComputerBackupJobSession
$sessions | where {$_.CreationTime -ge $date} | sort CreationTime | Format-Table
Thanks,
Oleg
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Question - Backup Job check powershell for Last 24 hours

Post by oleg.feoktistov »

@morciod,

Could you, please, describe which specific fields do you mean?

Thanks,
Oleg
jbyoon
Novice
Posts: 6
Liked: never
Joined: Feb 06, 2020 12:07 pm
Full Name: YOON JOO BYOUNG
Contact:

Re: Question - Backup Job check powershell for Last 24 hours

Post by jbyoon »

@oleg.feoktistov
You are really great !!
Thanks for giving me tremendous help.

Thanks to your help, I created the final PowerShell script like the one below.

Code: Select all

$date = (Get-Date).AddHours(-24)
$sessions = Get-VBRComputerBackupJobSession
foreach ($PBackup_job in (Get-VBRComputerBackupJob | Select Name)) {
	$PBackup_job_name = $PBackup_job.Name
	write "------------   Physical Server Backup Job Name : $PBackup_job_name   ------------"
	$sessions | where {$_.CreationTime -ge $date} | sort CreationTime | Select CreationTime, endtime, result, state | Format-Table
	}
Image
jbyoon
Novice
Posts: 6
Liked: never
Joined: Feb 06, 2020 12:07 pm
Full Name: YOON JOO BYOUNG
Contact:

Re: Question - Backup Job check powershell for Last 24 hours

Post by jbyoon »

I created a PowerShell that outputs the status of two types of backup jobs(VM and physical server) within 24 hours.
Thanks to everyone who helped. :)

Code: Select all

Add-PSSnapin Veeampssnapin
$date = (Get-Date).AddHours(-24)
$VMsessions = Get-VBRBackupSession
$VMsessions | where {$_.CreationTime -ge $date} |Select  @{Name="Type";Expression={"VM"}},@{Name="JobName";Expression={$_.JobName}}, CreationTime, endtime, result, state | Sort CreationTime | Format-Table

$PMsessions = Get-VBRComputerBackupJobSession
foreach ($PBackup_job in (Get-VBRComputerBackupJob | Select Name)) {
	$PBackup_job_name = $PBackup_job.Name
	$PMsessions | where {$_.CreationTime -ge $date} | sort CreationTime | Select @{Name="Type";Expression={"PM"}},@{Name="JobName";Expression={$PBackup_job_name}},CreationTime, endtime, result, state | Format-Table
}
Image
Rajesh1210
Influencer
Posts: 10
Liked: never
Joined: Feb 22, 2021 3:31 pm
Full Name: Rajesh TK
Contact:

Re: Question - Backup Job check powershell for Last 24 hours

Post by Rajesh1210 »

Hi Oleg,

How to list the session for a specific creation time. I am trying the below, but it is not working.

Code: Select all

$session | where {$_.CreationTime -eq 4/16/2022}
oleg.feoktistov wrote: Mar 22, 2021 2:47 pm @jbyoon,

For some reason, the output of Get-VBRComputerBackupJobSession cannot be filtered and sorted directly.
Try the approach below:

Code: Select all

$date = (Get-Date).AddHours(-24)
$sessions = Get-VBRComputerBackupJobSession
$sessions | where {$_.CreationTime -ge $date} | sort CreationTime | Format-Table
Thanks,
Oleg
rennerstefan
Veeam Software
Posts: 627
Liked: 146 times
Joined: Jan 22, 2015 2:39 pm
Full Name: Stefan Renner
Location: Germany
Contact:

Re: Question - Backup Job check powershell for Last 24 hours

Post by rennerstefan »

One way would be:

Code: Select all

$sessions | where {$_.CreationTime.day -eq "20" -and $_.CreationTime.month -eq "4"  -and $_.CreationTime.year -eq "2022"}
Stefan Renner

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

Re: Question - Backup Job check powershell for Last 24 hours

Post by oleg.feoktistov »

HI Rajesh,

It is not working because you are trying to filter CreationTime of datetime type by a string value. There is no type conversion happenning in this case. Adding to the way Stefan mentioned, another one would be to parse a date from a string and then use it for filtering:

Code: Select all

$date = [Datetime]::Parse('4/16/2022')
$sessions | where {$_.CreationTime.Date -eq $date}
In this case make sure to filter by CreationTime.Date, not just CreationTime. Otherwise hours/minutes/seconds in $date won't be ignored, and you will likely receive no results unless some sessions were started precisely at a default time written to $date (12:00:00 AM).

Thanks,
Oleg
Rajesh1210
Influencer
Posts: 10
Liked: never
Joined: Feb 22, 2021 3:31 pm
Full Name: Rajesh TK
Contact:

Re: Question - Backup Job check powershell for Last 24 hours

Post by Rajesh1210 »

Hi Oleg,

Thanks for the response!

I used the above suggestion but it did not filter out anything. Here is my code,

Code: Select all

$job = Get-VBRJob -Name VMC2-PRDCPY-WIN08-GRP01
$session = Get-VBRBackupSession | Where {$_.jobId -eq $job.Id.Guid}
$date = [datetime]::Parse('4/22/2022')
$session | where {$_.CreationTime.Date -eq $date}
Not sure what is missing. Could you please help with this?
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Question - Backup Job check powershell for Last 24 hours

Post by oleg.feoktistov »

Hi Rajesh,

Do you mean that it didn't reflect anything or it reflected many more irrelevant sessions?
If it's the first scenario, it would likely mean that it is either something wrong on the steps of getting job and sessions or no session ran on that date. In this case I'd advise to check if $job and $session variables hold any values. If they do, please double check if any session ran on that day for the mentioned job.

Thanks,
Oleg
Rajesh1210
Influencer
Posts: 10
Liked: never
Joined: Feb 22, 2021 3:31 pm
Full Name: Rajesh TK
Contact:

Re: Question - Backup Job check powershell for Last 24 hours

Post by Rajesh1210 »

Hi Oleg,

$job and $session had values in it but not for the date I was querying for. I re-ran them to populate again. I am able to get what I looked for. Thanks a lot for the help!
Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests