Update AdHoc-Backup.ps1

This commit is contained in:
Mattia Tadini 2025-11-19 12:20:15 +00:00
parent b172a66ee5
commit 0e74d9ba6c

View File

@ -25,6 +25,7 @@ try {
$ScriptPath = $MyInvocation.MyCommand.Path
$ScriptDir = Split-Path -Parent $ScriptPath
$MailScript = Join-Path $ScriptDir 'send-mail.ps1'
# Layout dell'installer GUI (se presente): .\bin\{RClone,7Zip,conf}
$InstallerBinRoot = Join-Path $ScriptDir 'bin'
@ -693,34 +694,6 @@ function Apply-GenericRetention {
#endregion ==========================================================================
#region ============================= Email =========================================
function Send-ReportMail {
param([string]$Subject,[string]$Body)
if (-not $MailEnabled) { return }
try {
$smtp = New-Object System.Net.Mail.SmtpClient($MailSmtpHost, $MailSmtpPort)
$smtp.EnableSsl = $true
$smtp.DeliveryMethod = [System.Net.Mail.SmtpDeliveryMethod]::Network
$smtp.UseDefaultCredentials = $false
if ($MailUseAuth) {
$smtp.Credentials = New-Object System.Net.NetworkCredential($MailUser, $MailPassword)
}
$msg = New-Object System.Net.Mail.MailMessage
$msg.From = $MailFrom
foreach ($rcpt in @($MailTo)) {
if ($rcpt) { $msg.To.Add($rcpt) }
}
$msg.Subject = $Subject
$msg.Body = $Body
$smtp.Send($msg)
Write-Log INFO "[MAIL] Inviata correttamente."
} catch {
Write-Log ERROR "[MAIL] Errore invio: $_"
}
}
#endregion ==========================================================================
#region ============================= MAIN ==========================================
@ -802,7 +775,7 @@ try {
Write-Log INFO "Backup $status."
$summary.Insert(0, "ESITO: $status`r`n") | Out-Null
Send-ReportMail -Subject ($MailSubjectPref + "OK " + $HostName) -Body $summary.ToString()
if (Test-Path -LiteralPath $MailScript) { . $MailScript -Subject ($MailSubjectPref + "OK " + $HostName) -Body $summary.ToString() } else { Write-Log WARN "Script di invio mail non trovato: $MailScript" }
} catch {
$msg = $_.Exception.Message
@ -810,7 +783,7 @@ try {
$summary.Insert(0, "ESITO: FALLITO`r`n") | Out-Null
$summary.AppendLine("") | Out-Null
$summary.AppendLine("ERRORE: $msg") | Out-Null
Send-ReportMail -Subject ($MailSubjectPref + "ERRORE " + $HostName) -Body $summary.ToString()
if (Test-Path -LiteralPath $MailScript) { . $MailScript -Subject ($MailSubjectPref + "ERRORE " + $HostName) -Body $summary.ToString() } else { Write-Log WARN "Script di invio mail non trovato: $MailScript" }
throw
} finally {
$summary.AppendLine("") | Out-Null