PowerShell script exchange
Post Reply
Tsmartt
Lurker
Posts: 1
Liked: never
Joined: Mar 06, 2024 4:27 pm
Full Name: Steven Smartt
Contact:

Veeam Backup Copy Job last session

Post by Tsmartt »

Code: Select all

# Initialize an empty array for backup jobs
$backupJobs = @()

# Retrieve all backup jobs from VBRJob and add them to the array
$backupJobs += Get-VBRJob

# Initialize a counter for jobs checked
$jobsChecked = 0

# Initialize a variable to store the messages
$messages = ""

foreach ($job in $backupJobs) {
    # Increment the jobs checked counter
    $jobsChecked++

    # Get the last session of the job
    $lastSession = $job.FindLastSession()
    # Check if the last session was successful
    if ($lastSession.Result -eq "Success" -xor $lastSession.State -eq "Success") {
        # Calculate the difference between now and the last successful backup
        $currentDate = Get-Date
        $timeDifference = $currentDate - $lastSession.EndTime

        # Initialize successWindow to 1 by default
        $successWindow = 1

        # Adjust successWindow based on job name
        if ($job.Name -like "*Daily*") {
            $successWindow = 1
        } elseif ($job.Name -like "*Weekly*") {
            $successWindow = 7
        } elseif ($job.Name -like "*Offsite*") {
            $successWindow = 7
        }

        # Check against the determined success window
        if ($timeDifference.TotalDays -gt $successWindow) {
            $messages += "Alert: Job $($job.Name) last success is greater than $successWindow day(s).`n"
        } else {
            $messages += "Job $($job.Name) is within the $successWindow-day success window.`n"
        }
    } else {
        $messages += "Job $($job.Name) did not complete successfully last time. `n" 
    }
}

# Output the messages
# Split $messages into an array of strings, one for each line/message
$messageArray = $messages -split "`n"

# Sort the array in descending order
$sortedMessages = $messageArray | Sort-Object -Descending

# If you want to convert it back to a single string with line breaks
$sortedMessagesString = $sortedMessages -join "`n"

# Output the sorted messages using Ninja-Property-Set
Ninja-Property-Set managedserverbackupcheck $sortedMessagesString
Hey Guys,
Im using the script above to try and get the last date of jobs and see if it is longer than 1 day or 1 week so that I can be alerted. The script is working as intended for HyperV jobs and Agent jobs but for some reason it keeps triggering alerts for backup copy jobs. The backup copy jobs are definetely completing in the specified time frame but they still trigger, so I'm really not sure whats going on. Anyone have any advice? Thanks!
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 6 guests