Skip to content

Instantly share code, notes, and snippets.

@dimiboy
Created May 2, 2020 12:13
Show Gist options
  • Save dimiboy/7a3a38a0b6c7886df4214c26b36490e3 to your computer and use it in GitHub Desktop.
Save dimiboy/7a3a38a0b6c7886df4214c26b36490e3 to your computer and use it in GitHub Desktop.
Windows Backup Script
########################################################
# #
# Author: Dima K. #
# YouTube: https://youtube.com/c/dimustech #
# #
########################################################
$SendMail = $true
$from = "john@mail.ru"
$pass = "MY-PASSWORD"
$to = ("john1@gmail.com", "john2@gmail.com")
$SmtpServer = "smtp.mail.ru"
$backuptarget = "\\qnap\Windows-Backup"
#================================================================================================
$secpasswd = (ConvertTo-SecureString "$pass" -AsPlainText -Force)
$mycreds = New-Object System.Management.Automation.PSCredential ($from, $secpasswd)
$out = (wbadmin start backup -backuptarget:$backuptarget -allcritical -quiet)
$backupdate = get-date
$outstring = $out | out-string
if($out -contains "The backup operation successfully completed.") #Success
{
if ($SendMail)
{
$Subject = "Windows backup succeeded"
$Body = "Windows backup succeeded at $backupdate `n`n $outstring"
Send-MailMessage -To $to -From $from -Subject $Subject -Body $Body -SmtpServer $SmtpServer -UseSsl -credential $mycreds
exit 0
}else {
exit 0
}
}else{ #Failed
if ($SendMail)
{
$Subject = "Windows backup Failed"
$Body = "Windows backup Failed at $backupdate `n`n $outstring"
Send-MailMessage -To $to -From $from -Subject $Subject -Body $Body -SmtpServer $SmtpServer -UseSsl -credential $mycreds
exit 1
}else {
exit 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment