Advertisement
MarkMeszaros

SEND EMAIL IF THE FOLDER CHANGES

Mar 3rd, 2024 (edited)
1,490
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ##########################################################################################################
  2. #                                                                                                       ##
  3. # Created by Mark Meszaros on March 1st, 2024.                                                          ##
  4. #                                                                                                       ##
  5. ##########################################################################################################
  6. #                                                                                                       ##
  7. # MODIFY THESE ROWS: 29, 38, 43*, 49*, 61*, 70*, 78*, 79*, 115, 116, 117                                ##
  8. #                                                                                                       ##
  9. # YOU DON'T HAVE TO MODIFY THE * MARKED ROWS IF NOT NECESSARY                                           ##
  10. #                                                                                                       ##
  11. ##########################################################################################################
  12. #                                                                                                       ##
  13. # RUN THE SCRIPT WITH THIS COMMAND AS AN ADMINISTRATOR IN CMD                                           ##
  14. # powershell -noexit -executionpolicy bypass -File C:\...\folderMonitor.ps1                             ##
  15. #                                                                                                       ##
  16. # YOU CAN NAME THIS SCRIPT WHATEVER YOU WANT... [folderMonitor.ps1]                                     ##
  17. #                                                                                                       ##
  18. ##########################################################################################################
  19. #                                                                                                       ##
  20. # NO ACCENTED CHARACTERS ALLOWED IN THIS CODE [PATH, FILENAME, SUBJECT, BODY, ETC...]                   ##
  21. #                                                                                                       ##
  22. ##########################################################################################################
  23. #                                                                                                       ##
  24. # ONLY OUTLOOK ACCOUNT WORKS WITH THE SCRIPT, FROM 2024 IT IS NOT POSSIBLE TO USE IT WITH GMAIL         ##
  25. #                                                                                                       ##
  26. ##########################################################################################################
  27. ##########################################################################################################
  28.                                                                                                         ##
  29. $Username = "something@outlook.com";                                                                    ##
  30. $Credential = Get-Credential -UserName $Username -Message "Enter your password"                         ##
  31.                                                                                                         ##
  32. $Password = $Credential.GetNetworkCredential().Password                                                 ##
  33.                                                                                                         ##
  34. ##########################################################################################################
  35. # CHANGE THIS PART OF THE CODE                                                                          ##
  36. ##########################################################################################################
  37.                                                                                                         ##
  38. $Path = "C:\...\MonitoredFolder";                                                                       ##
  39.                                                                                                         ##
  40. ##########################################################################################################
  41.                                                                                                         ##
  42. # FILTER EXAMPLES '*' '*.pdf.' '*.txt'                                                                  ##
  43. $FileFilter = '*'                                                                                       ##
  44.                                                                                                         ##
  45. ##########################################################################################################
  46.  
  47. $IncludeSubfolders = $true
  48.  
  49. $AttributeFilter = [IO.NotifyFilters]::FileName, [IO.NotifyFilters]::LastWrite
  50.  
  51. # NotifyFilters.Attributes
  52. # NotifyFilters.CreationTime
  53. # NotifyFilters.DirectoryName
  54. # NotifyFilters.FileName
  55. # NotifyFilters.LastAccess
  56. # NotifyFilters.LastWrite
  57. # NotifyFilters.Security
  58. # NotifyFilters.Size;
  59.  
  60.  
  61. $ChangeTypes = [System.IO.WatcherChangeTypes]::Created #, [System.IO.WatcherChangeTypes]::Deleted, ...
  62.  
  63. # YOU CAN ALSO USE THESE FILTERS
  64. # $ChangeTypes = [System.IO.WatcherChangeTypes]::All
  65. # $ChangeTypes = [System.IO.WatcherChangeTypes]::Created
  66. # $ChangeTypes = [System.IO.WatcherChangeTypes]::Changed
  67. # $ChangeTypes = [System.IO.WatcherChangeTypes]::Renamed
  68. # $ChangeTypes = [System.IO.WatcherChangeTypes]::Deleted # NOT WORKS WITH THE ATTACHMENT
  69.  
  70. $Timeout = 1000 # MILLISECONDS [1 SEC]
  71.  
  72.  
  73. function SendToEmail([string]$email, [string]$attachmentpath){
  74.  
  75.     $message = new-object Net.Mail.MailMessage;
  76.     $message.From = $Username
  77.     $message.To.Add($email);
  78.     $message.Subject = "Folder Changes";
  79.     $message.Body = "There has been a change in the monitorized folder.";
  80.     $attachment = New-Object Net.Mail.Attachment($attachmentpath);
  81.     $message.Attachments.Add($attachment);
  82.  
  83.     $smtp = new-object Net.Mail.SmtpClient("smtp.office365.com", "25");
  84.     $smtp.EnableSSL = $true;
  85.     $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
  86.     $smtp.send($message);
  87.     write-host "Mail Sent to $email" ;
  88.     $attachment.Dispose();
  89. }
  90.  
  91. function FileMonitoring
  92. {
  93.   param
  94.   (
  95.     [Parameter(Mandatory)]
  96.     [System.IO.WaitForChangedResult]
  97.     $ChangeInformation
  98.   )
  99.  
  100.   Write-Host 'Change detected:'
  101.   $ChangeInformation | Out-String | Write-Host -ForegroundColor DarkYellow
  102.    
  103.   $FullFilePath = $Path
  104.  
  105.   $FullFilePath += '\'
  106.  
  107.   $FullFilePath += $ChangeInformation.Name
  108.  
  109.   Write-Host $FullFilePath
  110.  
  111. ##########################################################################################################
  112. # CHANGE THIS PART OF THE CODE TO THE DESTINATION EMAILS                                                ##
  113. ##########################################################################################################
  114.                                                                                                         ##
  115.   SendToEmail -email "something@gmail.com" -attachmentpath $FullFilePath;                               ##
  116.   #SendToEmail -email "something@outlook.com" -attachmentpath $FullFilePath;                            ##
  117.   #SendToEmail -email "something@yahoo.com" -attachmentpath $FullFilePath;                              ##
  118.                                                                                                         ##
  119. ##########################################################################################################
  120.  
  121. }
  122.  
  123. try
  124. {
  125.   Write-Host "FileSystemWatcher is monitoring $Path"
  126.  
  127.  
  128.   $watcher = New-Object -TypeName IO.FileSystemWatcher -ArgumentList $Path, $FileFilter -Property @{
  129.     IncludeSubdirectories = $IncludeSubfolders
  130.     NotifyFilter = $AttributeFilter
  131.   }
  132.  
  133.  
  134.   do
  135.   {
  136.  
  137.     $result = $watcher.WaitForChanged($ChangeTypes, $Timeout)
  138.  
  139.     if ($result.TimedOut) { continue }
  140.    
  141.     FileMonitoring -Change $result
  142.    
  143.   } while ($true)
  144. }
  145. finally
  146. {
  147.   $watcher.Dispose()
  148.   Write-Host 'FileSystemWatcher removed.'
  149. }
  150.  
  151. # YOU CAN CLOSE THE SCRIPT WITH CTRL-C
  152. # ENJOY!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement