Advertisement
Guest User

Powershell System Image

a guest
Jul 12th, 2015
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. $wbAdminResult = ""
  15.  
  16. for($i = 0; $i -lt $MaxRetry; $i++) {
  17.     try{
  18.         $Errors = "none"
  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. }
  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.     $Subject = "Backup Failed!"
  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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement