Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # create password file with 'read-host -assecurestring | convertfrom-securestring | out-file C:\gmailSecureString.txt'
- # For gmail, enable 'insecure' authentication
- $ErrorId = 5
- $SuccessId = 14
- $MaxRetry = 2
- $From = "YourEmail@gmail.com"
- $To = "YourEmail@gmail.com"
- $SMTPServer = "smtp.gmail.com"
- $SMTPPort = "587"
- $PasswordSecureString = cat C:\gmailSecureString.txt | ConvertTo-SecureString
- $Credentials = new-object -TypeName System.Management.Automation.PSCredential -ArgumentList $From, $PasswordSecureString
- $wbAdminResult = ""
- for($i = 0; $i -lt $MaxRetry; $i++) {
- try{
- $Errors = "none"
- $wbAdminResult = [string] (cmd /c 'wbAdmin Start Backup -backupTarget:"I:" -include:"C:,M:" -allCritical -quiet 2>&1')
- if( !$wbAdminResult.Contains('ERROR') ){
- $i = $maxRetry
- }
- sleep(10)
- }catch{
- $Errors = $_
- }
- }
- $Results = Get-WinEvent -LogName Microsoft-Windows-Backup -MaxEvents 1
- if($Results.Id -eq $SuccessId -and $Errors -eq "none" -and !$wbAdminResult.Contains('ERROR')) {
- $Subject = "Backup Success"
- } else {
- $Subject = "Backup Failed!"
- }
- $Body = ""
- $Body += ($Results | Format-Table | Out-String)
- $Body += "`r`nErrors:`t`t" + $Errors
- $Body += "`r`nwbAdmin:`t" + $wbAdminResult
- Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl -Credential $Credentials
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement