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("<","<")).Replace(">",">").replace(""",'"').replace("'",'"') }
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("<","<")).Replace(">",">").replace(""",'"').replace("'",'"') }
$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