Cisco_root

Multi-Path FileWatcher

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