Cisco_root

Multi-Path FileWatcher

May 30th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #By BigTeddy 05 September 2011
  2.  
  3. #Modified by Mohamed Said "MohamedSaid82"
  4.  
  5. #This script uses the .NET FileSystemWatcher class to monitor file events in folder(s).
  6. #The advantage of this method over using WMI eventing is that this can monitor sub-folders.
  7. #The -Action parameter can contain any valid Powershell commands.  I have just included two for example.
  8. #The script can be set to a wildcard filter, and IncludeSubdirectories can be changed to $true.
  9. #You need not subscribe to all three types of event.  All three are shown for example.
  10. # Version 1.1
  11.  
  12. # SMTP Server information
  13.  
  14. $SMTPServer = "mail.test.com"
  15. $SMTPPort = "25"
  16. $username = "[email protected]"
  17. $password = "abc_123" | ConvertTo-SecureString -AsPlainText -Force
  18. $cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
  19.  
  20. # for every change for every monitored path the SourceIdentifier will change .
  21.  
  22. $i=0
  23.  
  24. # list of paths of the monitored folders . this path sould exist on your computer
  25.  
  26. $paths = Get-Content "C:\Scripts\Folder_monitored_paths.txt";
  27.  
  28. # list of users to send email to upon file or folder change . this path sould exist on your computer
  29.  
  30. $users = Get-Content "C:\Scripts\users.txt";
  31.  
  32.  
  33.  
  34. foreach ($path in $paths)
  35.  
  36. {
  37.  
  38. $filter = '*.*'  # You can enter a wildcard filter here.
  39.  
  40. # In the following line, you can change 'IncludeSubdirectories to $false if required to disable subfolder monitoring.                      
  41.  
  42. #  $fsw  : file system watcher variable can be either : option 1 to monitor only files or option 2 to monitor files and folders
  43.  
  44. # option 1 : $fsw = New-Object IO.FileSystemWatcher $path, $filter -Property @{IncludeSubdirectories = $tru;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
  45.  
  46. # option 2 : $fsw = New-Object IO.FileSystemWatcher $path -Property @{IncludeSubdirectories = $true}
  47.  
  48. $fsw = New-Object IO.FileSystemWatcher $path -Property @{IncludeSubdirectories = $true}
  49.  
  50. # Here, all four events are registerd.  You need only subscribe to events that you need:
  51.  
  52. Register-ObjectEvent $fsw Created -SourceIdentifier "$i+folderCreated" -Action {
  53.  
  54. $name = $Event.SourceEventArgs.Name
  55. $changeType = $Event.SourceEventArgs.ChangeType
  56. $fpath = $Event.SourceEventArgs.FullPath
  57. $timeStamp = $Event.TimeGenerated
  58.  
  59. # for tesing purpose , you can enable the following command to see the changes in an interactive console.
  60.  
  61. #Write-Host "The folder "$Event.SourceEventArgs.FullPath" was $changeType at $timeStamp" -fore green
  62.  
  63. #the follwing will log changes to a log file.
  64.  
  65. Out-File -FilePath c:\log\outlog.txt -Append -InputObject "The folder $fpath was $changeType at $timeStamp"
  66.    
  67. # the following for loop will send changes to every user lised in the $users variable indicationg the change that has happened.
  68.  
  69.     foreach ($user in $users)
  70.     {
  71.        
  72. $To = $user
  73. $Attachment = "C:\log\outlog.txt"
  74. $Subject = "New Folder has been Created"
  75. $Body = "Kindly be informed that a New Folder has been Created in the following path $fpath please consider a backup for this folder "
  76. send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -Attachments $Attachment -Credential $cred
  77.        
  78.     }
  79. }
  80.  
  81.  
  82.  
  83. Register-ObjectEvent $fsw changed -SourceIdentifier "$i+folderchanged" -Action {
  84.  
  85. $name = $Event.SourceEventArgs.Name
  86. $changeType = $Event.SourceEventArgs.ChangeType
  87. $fpath = $Event.SourceEventArgs.FullPath
  88. $timeStamp = $Event.TimeGenerated
  89.  
  90. #Write-Host "The folder "$Event.SourceEventArgs.FullPath" was $changeType at $timeStamp" -fore green
  91.  
  92. Out-File -FilePath c:\log\outlog.txt -Append -InputObject "The folder $fpath was $changeType at $timeStamp"
  93.    
  94.     foreach ($user in $users)
  95.     {
  96.        
  97. $To = $user
  98. $Attachment = "C:\log\outlog.txt"
  99. $Subject = "New Folder has been changed"
  100. $Body = "Kindly be informed that a New Folder has been changed in the following path $fpath please consider that "
  101.  
  102. send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -Attachments $Attachment -Credential $cred
  103.        
  104.     }
  105. }
  106.  
  107. Register-ObjectEvent $fsw Deleted -SourceIdentifier "$i+folderDeleted" -Action {
  108.  
  109. $name = $Event.SourceEventArgs.Name
  110. $changeType = $Event.SourceEventArgs.ChangeType
  111. $fpath = $Event.SourceEventArgs.FullPath
  112. $timeStamp = $Event.TimeGenerated
  113.  
  114. #Write-Host "The folder "$Event.SourceEventArgs.FullPath" was $changeType at $timeStamp" -fore green
  115. Out-File -FilePath c:\log\outlog.txt -Append -InputObject "The folder $fpath was $changeType at $timeStamp"
  116.    
  117.  
  118.     foreach ($user in $users)
  119.     {
  120.        
  121. $To = $user
  122. $Attachment = "C:\log\outlog.txt"
  123. $Subject = "a folder has been deleted"
  124. $Body = "Kindly be informed that a New Folder has been Deleted in the following path $fpath please consider that change "
  125.  
  126. send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -Attachments $Attachment -Credential $cred
  127.        
  128.     }
  129. }
  130.  
  131.  
  132.  
  133. Register-ObjectEvent $fsw Renamed -SourceIdentifier "$i+folderRenamed" -Action {
  134.  
  135. $name = $Event.SourceEventArgs.Name
  136. $changeType = $Event.SourceEventArgs.ChangeType
  137. $fpath = $Event.SourceEventArgs.FullPath
  138. $timeStamp = $Event.TimeGenerated
  139.  
  140. #Write-Host "The folder "$Event.SourceEventArgs.FullPath" was $changeType at $timeStamp" -fore green
  141. Out-File -FilePath c:\log\outlog.txt -Append -InputObject "The folder $fpath was $changeType at $timeStamp"
  142.    
  143.  
  144.     foreach ($user in $users)
  145.     {
  146.        
  147. $To = $user
  148. $Attachment = "C:\log\outlog.txt"
  149. $Subject = "a folder has been renamed"
  150. $Body = "Kindly be informed that a New Folder has been renamed in the following path $fpath please consider that change "
  151.  
  152. send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -Attachments $Attachment -Credential $cred
  153.        
  154.     }
  155. }
  156.  
  157.  
  158.  
  159.  
  160. # To stop the monitoring, run the following commands:
  161. # Unregister-Event FileDeleted
  162. # Unregister-Event FileCreated
  163. # Unregister-Event FileChanged
  164.  
  165.  
  166.  
  167.  
  168.  
  169. $i = $i+1
  170.  
  171.      }
  172.  
  173. # to unregister the events uncomment the following lines and create a new ps script
  174.  
  175. #$paths = Get-Content "C:\Scripts\Folder_monitored_paths.txt";
  176. #$i=0
  177.  
  178.  
  179. #foreach ($path in $paths)
  180.  
  181. #{
  182.  
  183.  
  184. #Unregister-Event "$i+FolderDeleted"
  185. #Unregister-Event "$i+FolderCreated"
  186. #Unregister-Event "$i+FolderChanged"
  187. #Unregister-Event "$i+FolderRenamed"
  188.  
  189. #$i=$i+1
  190.  
  191. #}
Add Comment
Please, Sign In to add comment