Log your errors directly into your Veeam job

Sometimes it is necessary to use job pre and post scripts. Most customers don’t like scripts because they have no good error logging and when the script has an error log you need to check this on regular bases. Would it be cool if you can get feedback from your pre- or post-job script directly into your Veeam Backup and Replication job.

I started to figure out where the log events for a job can be accessed by Powershell and after a while I found some C# methods of Powershell objects which looked like they can be used to log some events to VBR. To make this functions easier accessible I wrote some functions for my Powershell modules project. Take a look how this can be used.

Screenshot of my test job

I was able to implement Success, Warning, Error and Running result for an action as well as finish the running action. You see the first three “Marco Test *” actions where directly the action is written to log. You can see it because there is no duration after the action. the last one is a running actions. This action will be started with “Doing something…” and when it’s done you can update the action to the
message with the current result. I will explain with some examples how it can be done later in this post.

Because this are functions / methods which are not documented I cannot guarantee that they will always work. To simplify the usage I added it to my Powershell modules I provide at https://github.com/marcohorstmann/psscripts/. See Write your own Powershell modules for details about my sample script structure.

Did you ever tried to figure out inside of a Powershell script from which job it was started? First I thought maybe there is a variable but after digging into it I quickly realized that I need another way. I was happy to find an idea in Veeam R&D forums how to get with the Powershell process id the currently running job. Good thing is for this I created an Powershell function to simplify the use.

This is done by just calling the function Get-MHOVbrJobSessionFromPID and saving the result in a $BackupSession variable to use it later for logging messages to the job action log.

$BackupSession = Get-MHOVbrJobSessionFromPID

After this we can start to log messages to to job log. Writing the result to the variable is not required in this case, but I had strange stuff happening in powershell and returns of functions when not doing it in this way.

# Success
$logentry = Add-MHOVbrJobSessionLogEvent -BackupSession $BackupSession -Status Success -Text "Marco Test Success"

# Warning
$logentry = Add-MHOVbrJobSessionLogEvent -BackupSession $BackupSession -Status Warning -Text "Marco Test Warning"

# Error
$logentry = Add-MHOVbrJobSessionLogEvent -BackupSession $BackupSession -Status Error -Text "Marco Test Error"

In this example we start a longer running operation which will end with a warning after pausing the script for 10 seconds.

$logentry = Add-MHOVbrJobSessionLogEvent -BackupSession $BackupSession -Status Running -Text "Marco Test Running"
Start-Sleep -Seconds 10
$logentry = Add-MHOVbrJobSessionLogEvent -BackupSession $BackupSession -Status UpdateWarning -Text "Marco Test Finshed" -LogNumber $logentry

For more examples please take a look into the logExample.ps1 in the logExample folder of my github repository.

I hope this is helpful for you and maybe raise the acceptance for scripts.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

3 × 4 =