94 lines
3.5 KiB
PowerShell
94 lines
3.5 KiB
PowerShell
<#
|
||
************************************************************************************
|
||
PowerShell script that checks for disabled jobs and sends an e-mail containing the list
|
||
of disabled Veeam jobs.
|
||
************************************************************************************
|
||
#>
|
||
Add-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
|
||
$ScriptDir = 'C:\setup\script'
|
||
$JobExclusionFilterFile = "$ScriptDir\NotifyOfDisabledJobs_EXCLUSIONS.IN"
|
||
$MailTo = @(
|
||
"KrapivinAA@nornik.ru"
|
||
"RudenkoVV@nornik.ru"
|
||
"smolinmv@nornik.ru"
|
||
)
|
||
$MailFrom = "smolinmv@nornik.ru"
|
||
$MailSubject = "ALERT: Veeam jobs disabled"
|
||
$SMTPServer = ""
|
||
$SMTPPort = 587
|
||
$SMTPUser = "smolinmv@nornik.ru"
|
||
$SMTPPass = ""
|
||
$SMTPUseSSL = $true
|
||
# Использовать TLS 1.2
|
||
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
||
$DisabledJobs = Get-VBRJob | Where-Object {
|
||
$_.Info.IsScheduleEnabled -eq $false
|
||
}
|
||
if ($DisabledJobs) {
|
||
if ((Test-Path $JobExclusionFilterFile) -and
|
||
(-not [string]::IsNullOrWhiteSpace((Get-Content $JobExclusionFilterFile)))) {
|
||
$JobsToExclude = Get-Content $JobExclusionFilterFile
|
||
$DisabledJobsWithExclusions = $DisabledJobs.Name |
|
||
Select-String -Pattern $JobsToExclude -NotMatch |
|
||
Out-String
|
||
$JobsToExclude = $JobsToExclude | Out-String
|
||
$FinalReport = $DisabledJobsWithExclusions
|
||
}
|
||
else {
|
||
Write-Host "No job exclusion filter set"
|
||
$JobsToExclude = "None"
|
||
$FinalReport = $DisabledJobs.Name | Out-String
|
||
}
|
||
$EmailBody = @"
|
||
Пожалуйста, проверьте следующие отключенные задания Veeam и повторно включите их, повторно включите или удалите расписание, либо добавьте их в файл списка исключений.:
|
||
$FinalReport
|
||
Задания, имеющие следующие поисковые фильтры, исключаются из отчета.:
|
||
$JobsToExclude
|
||
"@
|
||
Write-Host $EmailBody
|
||
$SMTPCredential = New-Object System.Management.Automation.PSCredential(
|
||
$SMTPUser,
|
||
(ConvertTo-SecureString $SMTPPass -AsPlainText -Force)
|
||
)
|
||
# Отключаем проверку сертификата
|
||
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {
|
||
param($sender, $certificate, $chain, $sslPolicyErrors)
|
||
return $true
|
||
}
|
||
try {
|
||
$mail = New-Object System.Net.Mail.MailMessage
|
||
$mail.From = $MailFrom
|
||
foreach ($Recipient in $MailTo) {
|
||
$mail.To.Add($Recipient)
|
||
}
|
||
$mail.Subject = $MailSubject
|
||
$mail.Body = $EmailBody
|
||
$mail.IsBodyHtml = $false
|
||
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort)
|
||
$smtp.EnableSsl = $SMTPUseSSL
|
||
$smtp.Credentials = $SMTPCredential
|
||
$smtp.Send($mail)
|
||
Write-Host "E-mail sent successfully." -ForegroundColor Green
|
||
}
|
||
catch {
|
||
Write-Host "SMTP Error:" -ForegroundColor Red
|
||
Write-Host $_.Exception.Message -ForegroundColor Red
|
||
if ($_.Exception.InnerException) {
|
||
Write-Host $_.Exception.InnerException.Message -ForegroundColor Yellow
|
||
}
|
||
}
|
||
finally {
|
||
# Возвращаем стандартную проверку сертификатов
|
||
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = $null
|
||
if ($mail) {
|
||
$mail.Dispose()
|
||
}
|
||
if ($smtp) {
|
||
$smtp.Dispose()
|
||
}
|
||
}
|
||
}
|
||
else {
|
||
Write-Host "No disabled jobs"
|
||
}
|