From efe71c0417140b3ddcd76edf8271f5465b215348 Mon Sep 17 00:00:00 2001 From: zwnk Date: Mon, 3 Dec 2018 18:11:33 +0100 Subject: [PATCH] Update 'Virtual_box_backup.ps1' --- Virtual_box_backup.ps1 | 45 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/Virtual_box_backup.ps1 b/Virtual_box_backup.ps1 index cca97ed..52b2ea6 100644 --- a/Virtual_box_backup.ps1 +++ b/Virtual_box_backup.ps1 @@ -12,8 +12,23 @@ $remoteBackupPath = "\\freenas\programas\VM_Backup" $backupDriveLetterMount = "L" $backupFilename = $backupDriveLetterMount + ":\" + [string](Get-Date).Year + [string](Get-Date).Month + [string](Get-Date).Day + "_Intranet_Backup.ova" $vmName = "2018_intranet" -$logFile = "c:\logs\vm_backup.log" +$logFile = "C:\Users\administrador.DMCL\Documents\vmbackup\VMBackup.log" +# Email Setup +$EmailTo1 = "" +$EmailTo2 = "" +$SMTPServer = "smtp.gmail.com" +$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) +$SMTPClient.EnableSsl = $true +$user = "xxx@gmail.com" +$pw = ConvertTo-SecureString "Password" -AsPlainText -Force +$SMTPClient.Credentials = New-Object System.Management.Automation.PSCredential($user, $pw) +$emailMessage = New-Object System.Net.Mail.MailMessage +$emailMessage.From = $user +$emailMessage.To.Add($EmailTo1) +$emailMessage.To.Add($EmailTo2) + +# Appending new header line to log file $Time=Get-Date "################ Starting Intranet Backup at $Time ######################" | out-file $logFile -append @@ -30,6 +45,10 @@ Catch "[Error] Mounting Backup Path at $Time" | out-file $logFile -append $_.Exception.Message | out-file $logFile -append $_.Exception.ItemName | out-file $logFile -append + $emailMessage.Subject = "Error Mounting Backup Path" + $emailMessage.Body = "Error Mounting Backup Path $Time" + $emailMessage.Attachments.Add($logFile) + $SMTPClient.Send($emailMessage) Break } @@ -51,6 +70,10 @@ Catch "[Error] to Stop VM at $Time" | out-file $logFile -append $_.Exception.Message | out-file $logFile -append $_.Exception.ItemName | out-file $logFile -append + $emailMessage.Subject = "Error Stopping VM Intranet" + $emailMessage.Body = "Error Stopping VM Intranet at $Time" + $emailMessage.Attachments.Add($logFile) + $SMTPClient.Send($emailMessage) Break } @@ -74,6 +97,10 @@ Catch "[Error] Backing Up VM at $Time" | out-file $logFile -append $_.Exception.Message | out-file $logFile -append $_.Exception.ItemName | out-file $logFile -append + $emailMessage.Subject = "Error Creating Backup VM Intranet" + $emailMessage.Body = "Error Creating Backup at $Time" + $emailMessage.Attachments.Add($logFile) + $SMTPClient.Send($emailMessage) Break } @@ -95,6 +122,10 @@ Catch "[Error] Starting VM at $Time" | out-file $logFile -append $_.Exception.Message | out-file $logFile -append $_.Exception.ItemName | out-file $logFile -append + $emailMessage.Subject = "Error Starting VM Intranet" + $emailMessage.Body = "Error Starting VM Intranet at $Time" + $emailMessage.Attachments.Add($logFile) + $SMTPClient.Send($emailMessage) Break } # remove old Backups @@ -117,7 +148,10 @@ DO "[Error] Deleting old backup at $Time" | out-file $logFile -append $_.Exception.Message | out-file $logFile -append $_.Exception.ItemName | out-file $logFile -append - Break + $emailMessage.Subject = "Error Deleting Old Backup VM Intranet" + $emailMessage.Body = "Error Deleting Old Backup VM Intranet at $Time" + $emailMessage.Attachments.Add($logFile) + $SMTPClient.Send($emailMessage) } $backupFileCount = ( Get-ChildItem -Filter *.ova $remoteBackupPath | Measure-Object ).Count } While ($backupFileCount -gt 3) @@ -133,6 +167,13 @@ Catch "[Error] Umounting Network drive at $Time" | out-file $logFile -append $_.Exception.Message | out-file $logFile -append $_.Exception.ItemName | out-file $logFile -append + $emailMessage.Subject = "Error Unmounting Backup Path" + $emailMessage.Body = "Error Unmounting Backup Path at $Time" + $emailMessage.Attachments.Add($logFile) + $SMTPClient.Send($emailMessage) Break } +$emailMessage.Subject = "Backup Intranet Done!" +$emailMessage.Body = "Backup Intranet Done! $Time" +$SMTPClient.Send($emailMessage) \ No newline at end of file