View difference between Paste ID: MBjiUh6y and ah6vLtKK
SHOW: | | - or go back to the newest paste.
1
# create password file with 'read-host -assecurestring | convertfrom-securestring | out-file C:\gmailSecureString.txt'
2
# For gmail, enable 'insecure' authentication
3
4
$ErrorId = 5
5
$SuccessId = 14
6
$MaxRetry = 2
7
8
$From = "YourEmail@gmail.com"
9
$To = "YourEmail@gmail.com"
10
$SMTPServer = "smtp.gmail.com"
11
$SMTPPort = "587"
12
$PasswordSecureString = cat C:\gmailSecureString.txt | ConvertTo-SecureString
13
$Credentials = new-object -TypeName System.Management.Automation.PSCredential -ArgumentList $From, $PasswordSecureString
14-
$Errors = "none"
14+
$wbAdminResult = ""
15
16-
for($i=0; $i -le $MaxRetry; $i++){
16+
for($i = 0; $i -lt $MaxRetry; $i++) {
17
    try{
18-
        wbAdmin Start Backup -backupTarget:"I:" -include:"C:,M:" -allCritical -quiet
18+
        $Errors = "none"
19-
        $i = $maxRetry
19+
        $wbAdminResult = [string] (cmd /c 'wbAdmin Start Backup -backupTarget:"I:" -include:"C:,M:" -allCritical -quiet 2>&1')
20
        if( !$wbAdminResult.Contains('ERROR') ){
21
             $i = $maxRetry
22
        }
23
        sleep(10)
24
    }catch{
25
        $Errors = $_
26
    }
27-
if($Results.Id -eq $SuccessId -and $Error -eq "none") {
27+
28
29
$Results = Get-WinEvent -LogName Microsoft-Windows-Backup -MaxEvents 1
30
31
if($Results.Id -eq $SuccessId -and $Errors -eq "none" -and !$wbAdminResult.Contains('ERROR')) {
32
    $Subject = "Backup Success"
33
} else {
34-
$Body = $Body + ($Results | Format-Table | Out-String)
34+
35-
$Body = $Body + "\r\n" + $Errors
35+
36
37
$Body = ""
38
$Body += ($Results | Format-Table | Out-String)
39
$Body += "`r`nErrors:`t`t" + $Errors
40
$Body += "`r`nwbAdmin:`t" + $wbAdminResult
41
Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl -Credential $Credentials