Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #By BigTeddy 05 September 2011
- #Modified by Mohamed Said "MohamedSaid82"
- #This script uses the .NET FileSystemWatcher class to monitor file events in folder(s).
- #The advantage of this method over using WMI eventing is that this can monitor sub-folders.
- #The -Action parameter can contain any valid Powershell commands. I have just included two for example.
- #The script can be set to a wildcard filter, and IncludeSubdirectories can be changed to $true.
- #You need not subscribe to all three types of event. All three are shown for example.
- # Version 1.1
- # SMTP Server information
- $SMTPServer = "mail.test.com"
- $SMTPPort = "25"
- $username = "Test1@test.com"
- $password = "abc_123" | ConvertTo-SecureString -AsPlainText -Force
- $cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
- $From = "fsrm@Test.Com"
- # for every change for every monitored path the SourceIdentifier will change .
- $i=0
- # list of paths of the monitored folders . this path sould exist on your computer
- $paths = Get-Content "C:\Scripts\Folder_monitored_paths.txt";
- # list of users to send email to upon file or folder change . this path sould exist on your computer
- $users = Get-Content "C:\Scripts\users.txt";
- foreach ($path in $paths)
- {
- $filter = '*.*' # You can enter a wildcard filter here.
- # In the following line, you can change 'IncludeSubdirectories to $false if required to disable subfolder monitoring.
- # $fsw : file system watcher variable can be either : option 1 to monitor only files or option 2 to monitor files and folders
- # option 1 : $fsw = New-Object IO.FileSystemWatcher $path, $filter -Property @{IncludeSubdirectories = $tru;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
- # option 2 : $fsw = New-Object IO.FileSystemWatcher $path -Property @{IncludeSubdirectories = $true}
- $fsw = New-Object IO.FileSystemWatcher $path -Property @{IncludeSubdirectories = $true}
- # Here, all four events are registerd. You need only subscribe to events that you need:
- Register-ObjectEvent $fsw Created -SourceIdentifier "$i+folderCreated" -Action {
- $name = $Event.SourceEventArgs.Name
- $changeType = $Event.SourceEventArgs.ChangeType
- $fpath = $Event.SourceEventArgs.FullPath
- $timeStamp = $Event.TimeGenerated
- # for tesing purpose , you can enable the following command to see the changes in an interactive console.
- #Write-Host "The folder "$Event.SourceEventArgs.FullPath" was $changeType at $timeStamp" -fore green
- #the follwing will log changes to a log file.
- Out-File -FilePath c:\log\outlog.txt -Append -InputObject "The folder $fpath was $changeType at $timeStamp"
- # the following for loop will send changes to every user lised in the $users variable indicationg the change that has happened.
- foreach ($user in $users)
- {
- $To = $user
- $Attachment = "C:\log\outlog.txt"
- $Subject = "New Folder has been Created"
- $Body = "Kindly be informed that a New Folder has been Created in the following path $fpath please consider a backup for this folder "
- send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -Attachments $Attachment -Credential $cred
- }
- }
- Register-ObjectEvent $fsw changed -SourceIdentifier "$i+folderchanged" -Action {
- $name = $Event.SourceEventArgs.Name
- $changeType = $Event.SourceEventArgs.ChangeType
- $fpath = $Event.SourceEventArgs.FullPath
- $timeStamp = $Event.TimeGenerated
- #Write-Host "The folder "$Event.SourceEventArgs.FullPath" was $changeType at $timeStamp" -fore green
- Out-File -FilePath c:\log\outlog.txt -Append -InputObject "The folder $fpath was $changeType at $timeStamp"
- foreach ($user in $users)
- {
- $To = $user
- $Attachment = "C:\log\outlog.txt"
- $Subject = "New Folder has been changed"
- $Body = "Kindly be informed that a New Folder has been changed in the following path $fpath please consider that "
- send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -Attachments $Attachment -Credential $cred
- }
- }
- Register-ObjectEvent $fsw Deleted -SourceIdentifier "$i+folderDeleted" -Action {
- $name = $Event.SourceEventArgs.Name
- $changeType = $Event.SourceEventArgs.ChangeType
- $fpath = $Event.SourceEventArgs.FullPath
- $timeStamp = $Event.TimeGenerated
- #Write-Host "The folder "$Event.SourceEventArgs.FullPath" was $changeType at $timeStamp" -fore green
- Out-File -FilePath c:\log\outlog.txt -Append -InputObject "The folder $fpath was $changeType at $timeStamp"
- foreach ($user in $users)
- {
- $To = $user
- $Attachment = "C:\log\outlog.txt"
- $Subject = "a folder has been deleted"
- $Body = "Kindly be informed that a New Folder has been Deleted in the following path $fpath please consider that change "
- send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -Attachments $Attachment -Credential $cred
- }
- }
- Register-ObjectEvent $fsw Renamed -SourceIdentifier "$i+folderRenamed" -Action {
- $name = $Event.SourceEventArgs.Name
- $changeType = $Event.SourceEventArgs.ChangeType
- $fpath = $Event.SourceEventArgs.FullPath
- $timeStamp = $Event.TimeGenerated
- #Write-Host "The folder "$Event.SourceEventArgs.FullPath" was $changeType at $timeStamp" -fore green
- Out-File -FilePath c:\log\outlog.txt -Append -InputObject "The folder $fpath was $changeType at $timeStamp"
- foreach ($user in $users)
- {
- $To = $user
- $Attachment = "C:\log\outlog.txt"
- $Subject = "a folder has been renamed"
- $Body = "Kindly be informed that a New Folder has been renamed in the following path $fpath please consider that change "
- send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -Attachments $Attachment -Credential $cred
- }
- }
- # To stop the monitoring, run the following commands:
- # Unregister-Event FileDeleted
- # Unregister-Event FileCreated
- # Unregister-Event FileChanged
- $i = $i+1
- }
- # to unregister the events uncomment the following lines and create a new ps script
- #$paths = Get-Content "C:\Scripts\Folder_monitored_paths.txt";
- #$i=0
- #foreach ($path in $paths)
- #{
- #Unregister-Event "$i+FolderDeleted"
- #Unregister-Event "$i+FolderCreated"
- #Unregister-Event "$i+FolderChanged"
- #Unregister-Event "$i+FolderRenamed"
- #$i=$i+1
- #}
Add Comment
Please, Sign In to add comment