PowerShell script exchange
Post Reply
Rajesh1210
Influencer
Posts: 12
Liked: never
Joined: Feb 22, 2021 3:31 pm
Full Name: Rajesh TK
Contact:

Monitor actively running job to take required action

Post by Rajesh1210 »

Hi Guys,

We would like to take actions like Sleep, hibernate or shutdown on VMs by checking the backup completion status of it in an agent based job. How would we place the script to monitor this active job?
Mildur
Product Manager
Posts: 9848
Liked: 2607 times
Joined: May 13, 2017 4:51 pm
Full Name: Fabian K.
Location: Switzerland
Contact:

Re: Monitor actively running job to take required action

Post by Mildur »

Was that your goal? Shutdown a vm after Job was completed?
Then you can leverage „after-job“ Scripts in the job Settings. No need to track job status with powershell :-)

https://helpcenter.veeam.com/docs/backu ... ml?ver=100

If you have to monitor the status of the completed job, then powershell will be needed. But i have todo a little research for finding the right commands.
Product Management Analyst @ Veeam Software
Rajesh1210
Influencer
Posts: 12
Liked: never
Joined: Feb 22, 2021 3:31 pm
Full Name: Rajesh TK
Contact:

Re: Monitor actively running job to take required action

Post by Rajesh1210 »

Hi Mildur,

Leveraging after-job Scripts in job settings can be done only if I have to wait until the backup job completes. But I would like to Turn off the VM once it is backed up in the job but not wait until the entire job completes.
oleg.feoktistov
Veeam Software
Posts: 2010
Liked: 670 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Monitor actively running job to take required action

Post by oleg.feoktistov »

Hi Rajesh,

Then in the script you'll need to listen to the specific task session and operate over VM once the task session is completed.
Here is an example for failed session. You can easily adjust it to your needs as per sessions statuses:

Code: Select all

$session = (Get-VBRComputerBackupJobSession) | where {$_.State -ne "Stopped"}
$taskSessions = Get-VBRTaskSession -Session $session[0]
foreach ($taskSession in $taskSessions) {
  $finished = $false
  while ($finished -eq $false) {
    $task = Get-VBRTaskSession -Session $session[0] | where {$_.Id -eq $taskSession.Id}
    if ($task.Status -eq "Failed") {
        Write-Host "The session with id: $($task.Id) has failed"
        # Stop VM/Shutdown VM here
        $finished = $true
    }
    else {
    Start-Sleep -Seconds 10
    Write-Host "Waiting for the session to fail"
    }
  }
} 

Here are 2 important points to highlight though:
  • Get-VBRTaskSession may not work properly with agent backup sessions. We are aware of that and noted as an enhancement.
  • There is a bug with Get-VBRComputerBackupJobSession cmdlet. If it obtains an array of objects, it reflects them as a list instead, so closing brackets are necessary for now. Noted as a bug.
Thanks,
Oleg
Rajesh1210
Influencer
Posts: 12
Liked: never
Joined: Feb 22, 2021 3:31 pm
Full Name: Rajesh TK
Contact:

Re: Monitor actively running job to take required action

Post by Rajesh1210 »

Thanks for the response, Oleg. Couple of questions on that,

1) What would be the Shutdown command?
2) Where would we place this script in Job settings? Because if the script is placed in Pre-Job, then Job has to wait until the script gets executed whereas if the script is placed in Post-Job, script doesn't get kicked off until the Job gets completed.
oleg.feoktistov
Veeam Software
Posts: 2010
Liked: 670 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Monitor actively running job to take required action

Post by oleg.feoktistov »

Hi Rajesh,

Sorry, I have been busy lately, but I haven't forgot about your questions.

1) I guess, you are referring to shutting down guest OS? Then it's this command. For direct operations on VMs through Powershell please refer to PowerCLI guide.

2) My first idea was to place it as a pre-job script and wrap the script itself in Start-Job cmdlet to make it start asynchronously and let the backup job continue, while a separate script job would run on the background and listen to the backup job session. However, when I approached it with some logging, turned out that though in the job session details the status of pre-job script execution is 'success', the background job won't log anything as if it was never started.
I'm testing it from different angles now. Will let you know as soon as I have a full picture.

Thanks,
Oleg
oleg.feoktistov
Veeam Software
Posts: 2010
Liked: 670 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Monitor actively running job to take required action

Post by oleg.feoktistov »

Since the core aim of pre-job script feature is to wait for successful completion of the script, there seem to be no direct way to setup it to run asynchronously.
However, you could configure a task in Windows Task Scheduler, which triggers Powershell script, and start this scheduler task on pre-job script stage. Thus, there will be a separate controller to monitor script completion after you received script exit code 0 in your backup job session log.

So, the steps are:
  • Create main powershell script,TaskMonitor.ps1:

    Code: Select all

     Start-Job -Name taskMonitor -ScriptBlock {
    $job = Get-VBRComputerBackupJob -Name 'Job'
    $session = (Get-VBRComputerBackupJobSession) | where {$_.State -ne "Stopped"}
    if (!$session) {
      Write-Output "No session running" | Out-File -FilePath "C:\log.txt"
    }
    else {
    $taskSessions = Get-VBRTaskSession -Session $session[0]
    foreach ($taskSession in $taskSessions) {
      $finished = $false
      while ($finished -eq $false) {
        $task = Get-VBRTaskSession -Session $session[0] | where {$_.Id -eq $taskSession.Id}
        if ($task.Status -eq "Success") {
            Write-Output "The session with id: $($task.Id) has succeeded" | Out-File -FilePath "C:\log.txt" -Append
            # More actions here
            $finished = $true
        }
        elseif ($task.Status -eq "Failed") {
            Write-Output "The session with id: $($task.Id) has failed" | Out-File -FilePath "C:\log.txt" -Append
            # More actions here
            $finished = $true
        }
        else {
        Start-Sleep -Seconds 10
        Write-Output "Waiting for the session to complete" | Out-File -FilePath "C:\log.txt" -Append
        }
      }
     }
    }
    }
    Wait-Job taskMonitor
    
  • Create a task called "taskMonitor" in Windows Task Scheduler, which triggers TaskMonitor.ps1 on manual run.
  • Create a powershell script to invoke task in Windows Task Scheduler, Invocation.ps1:

    Code: Select all

    Start-ScheduledTask -TaskName "taskMonitor" 
    
  • Choose Invocation.ps1 as a pre-job script.
TaskMonitor.ps1 is just a sample script, which logs task session completion status for the first task session to the file. If you want to operate over backed up entities, adjust it accordingly.

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

Re: Monitor actively running job to take required action

Post by Rajesh1210 »

Hi Oleg,

Thanks for the previous response. I am looking to get the latest job result for an agent job. Could you please help me with the command to get that? I am trying the below.

$Job = Get-VBRComputerBackupJob -Name VMC2-NPDBAK-WIN16-RDGRP01
$Session = $Job.FindLastSession()
Rajesh1210
Influencer
Posts: 12
Liked: never
Joined: Feb 22, 2021 3:31 pm
Full Name: Rajesh TK
Contact:

Re: Monitor actively running job to take required action

Post by Rajesh1210 »

Hi Oleg,

This seems to be interesting. Your this reply made me to think and better understand the exit codes. I would like you to confirm if I've understood it correctly.

"Backup job looks for the exit code from Pre-job script and Backup job sends the exit code to trigger the Post-job script."

And Will we able to find these exit codes in logs?

Thanks and Regards,
Rajesh TK
oleg.feoktistov
Veeam Software
Posts: 2010
Liked: 670 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Monitor actively running job to take required action

Post by oleg.feoktistov »

Hi Rajesh,

VBRComputerBackupJob type doesn't have FindLastSession() method as it is not an internal class.
To use FindLastSession() method you need to find your agent job with Get-VBRJob cmdlet:

Code: Select all

$job = Get-VBRJob -Name "Agent Job"
$session = $job.FindLastSession()
A for exit codes, yes, precisely, you can find them in a job log located in "C:\ProgramData\Veeam\Backup\<JobName>\Job.Job_Name.log"

Thanks,
Oleg
Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests