PowerShell script exchange
Post Reply
Kazz
Expert
Posts: 140
Liked: 17 times
Joined: Feb 08, 2018 3:47 am
Full Name: Kazz Beck
Contact:

Veeam Job Enabler/Disabler script

Post by Kazz » 8 people like this post

Hi,

I can't seem to find my post here with a script I wrote last year to Enable/Disable Veeam Jobs from a scheduled task. We usually run it before/after the scheduled maintenance on storage. Please delete if this is a duplicate, otherwise please comment here if you find it useful.

Script is expecting an argument to be supplied. Enable or Disable

Code: Select all


Add-PSSnapin VeeamPSSnapin

#Used for Debugging 
#$args=@("Enable")

# Configure variables in this section 
$smtpServ = "smtp"
$rcptFrom = "from@email.com"
$rcptTo = "to@email.com"
$eMailSubj = "Veeam Backup Jobs " + $args[0] + " Notification"
$VBRJobName = "*"
$logFile = "C:\Temp\ChangeVeeamJobs.log"

#Obtain Previous Job settings
$Jobs = Get-VBRJob -name $VBRJobName
$JobState = $Jobs | Select-Object @{N='Job Name';E={$_.Name}}, @{N="Schedule Enabled";E={Set-Color -string $_.IsScheduleEnabled}}, IsRunning, @{N="Next Run";E={Get-VBRJobScheduleOptions -Job $_ | Select NextRun -ExpandProperty NextRun}}

if($args[0] -eq "Enable") {
   $ChangeJobState = $Jobs | Enable-VBRJob 
}

if ($args[0] -eq "Disable"){
   $ChangeJobState = $Jobs | Disable-VBRJob 
   Start-Sleep -s 5
} 

#Obtain New Job settings
$Jobs = Get-VBRJob -name $VBRJobName 
$ChangeJobState = $Jobs | Select-Object @{N='Job Name';E={$_.Name}}, @{N="Schedule Enabled";E={Set-Color -string $_.IsScheduleEnabled}}, IsRunning, @{N="Next Run";E={Get-VBRJobScheduleOptions -Job $_ | Select NextRun -ExpandProperty NextRun}} 

# Write results to log file
$getCurTime = Get-Date | Out-File $logFile -Append
$JobState | Out-File  $logFile -Append


# HTML Header
$head = "<style>"
$head = $head + "BODY{background-color:white; font-size:10pt;font-family: 'Trebuchet MS', Arial, Helvetica, sans-serif} "
$head = $head + "table {margin-top:3px;border-collapse: collapse;width: 100%;} "
$head = $head + "table td, table th {border: 1px solid #ddd;text-align: left; padding: 7px; valign:top; font-size:10pt; height:10px; line-height:normal} "
$head = $head + "table tr:nth-child(even){background-color: #f2f2f2} "
$head = $head + "table tr:hover {background-color: #ddd;} "
$head = $head + "table th {padding-top: 3px;padding-bottom: 3px;background-color: #3B6E8F;color: white;} "
$head = $head + "table tr {line-height: 3px;} "
$head = $head + ".red {color:red;} " 
$head = $head + ".green {color:green;} "
$head = $head + "h4 {border-bottom:2px solid orangered;padding-bottom:5px;} "
$head = $head + "</style>" 

$PrevJobState = $JobState | ConvertTo-Html |% { ($_.Replace("&lt;","<")).Replace("&gt;",">").replace("&quot;",'"').replace("&#39;",'"') }

if ($args[0] -eq "" -or $args[0] -eq $null){
    $NewJobState = 'The script executed without a required parameter, resulting in no modifications. Please rerun the script, ensuring you specify the Enable or Disable parameter.'
    $eMailSubj = "Veeam Backup Jobs No Change Notification"
	$NewJobState | Out-File $logFile -Append
}
else{
    $NewJobState = $ChangeJobState | ConvertTo-Html |% { ($_.Replace("&lt;","<")).Replace("&gt;",">").replace("&quot;",'"').replace("&#39;",'"') }
    $ChangeJobState | Out-File $logFile -Append
}


# Set HTML color function

function Set-Color($string) {
    if ($string -eq $false){
        "<span class='red'>$string</span>"
    }
    elseif ($string -eq $true){
        "<span class='green'>$string</span>"
    }
    else{
        $string
    }
}


# Assemble email message.
$msgbody = "<html><head>" + $head + "</head><body>" + "<h4>Previous Configuration:</h4>" + $PrevJobState + "<h4>New Configuration:</h4>" +$NewJobState +"</body></html>"

# Email resulst.
Send-MailMessage -To $rcptTo -from $rcptFrom -Subject $eMailSubj -BodyAsHtml $msgbody -SmtpServer $smtpServ 




ortoscale
Service Provider
Posts: 246
Liked: 20 times
Joined: Aug 02, 2011 9:30 pm
Full Name: Matjaž Antloga
Location: Celje, Slovenia
Contact:

Re: Veeam Job Enabler/Disabler script

Post by ortoscale »

Hi there.

Great, but question arise. What you do with running jobs?
Kazz
Expert
Posts: 140
Liked: 17 times
Joined: Feb 08, 2018 3:47 am
Full Name: Kazz Beck
Contact:

Re: Veeam Job Enabler/Disabler script

Post by Kazz »

Running jobs will get disabled, but you will need to wait for them to finish. The script does not cancel any running jobs, additionally if any of the jobs are running you will see it on the email report produced by the script when it runs.

Comments:
- Add-PSSnapin VeeamPSSnapin - it's needed, and should be removed.
- function Set-Color - does not work when script is executed as a scheduled task, move the function to the top of the scrip to fix it
ortoscale
Service Provider
Posts: 246
Liked: 20 times
Joined: Aug 02, 2011 9:30 pm
Full Name: Matjaž Antloga
Location: Celje, Slovenia
Contact:

Re: Veeam Job Enabler/Disabler script

Post by ortoscale »

Thanks for explanation.
We are using ansible for updating servers and this could become handy to integrate.
cleberpenteado
Novice
Posts: 3
Liked: never
Joined: May 31, 2022 11:41 am
Contact:

Re: Veeam Job Enabler/Disabler script

Post by cleberpenteado »

Hi,

And how to authenticate SMTP?
Kazz
Expert
Posts: 140
Liked: 17 times
Joined: Feb 08, 2018 3:47 am
Full Name: Kazz Beck
Contact:

Re: Veeam Job Enabler/Disabler script

Post by Kazz » 1 person likes this post

If your SMTP requires authentication you would use -Credential parameter.

My SMTP is local with no authentication required, additionally if your SMTP requires modern authentication, email notifications will not work. Send-MailMessage is limited to basic authentication only.

https://learn.microsoft.com/en-us/power ... rshell-7.4
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 12 guests