Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##########################################################################################################
- # ##
- # Created by Mark Meszaros on March 1st, 2024. ##
- # ##
- ##########################################################################################################
- # ##
- # MODIFY THESE ROWS: 29, 38, 43*, 49*, 61*, 70*, 78*, 79*, 115, 116, 117 ##
- # ##
- # YOU DON'T HAVE TO MODIFY THE * MARKED ROWS IF NOT NECESSARY ##
- # ##
- ##########################################################################################################
- # ##
- # RUN THE SCRIPT WITH THIS COMMAND AS AN ADMINISTRATOR IN CMD ##
- # powershell -noexit -executionpolicy bypass -File C:\...\folderMonitor.ps1 ##
- # ##
- # YOU CAN NAME THIS SCRIPT WHATEVER YOU WANT... [folderMonitor.ps1] ##
- # ##
- ##########################################################################################################
- # ##
- # NO ACCENTED CHARACTERS ALLOWED IN THIS CODE [PATH, FILENAME, SUBJECT, BODY, ETC...] ##
- # ##
- ##########################################################################################################
- # ##
- # ONLY OUTLOOK ACCOUNT WORKS WITH THE SCRIPT, FROM 2024 IT IS NOT POSSIBLE TO USE IT WITH GMAIL ##
- # ##
- ##########################################################################################################
- ##########################################################################################################
- ##
- $Credential = Get-Credential -UserName $Username -Message "Enter your password" ##
- ##
- $Password = $Credential.GetNetworkCredential().Password ##
- ##
- ##########################################################################################################
- # CHANGE THIS PART OF THE CODE ##
- ##########################################################################################################
- ##
- $Path = "C:\...\MonitoredFolder"; ##
- ##
- ##########################################################################################################
- ##
- # FILTER EXAMPLES '*' '*.pdf.' '*.txt' ##
- $FileFilter = '*' ##
- ##
- ##########################################################################################################
- $IncludeSubfolders = $true
- $AttributeFilter = [IO.NotifyFilters]::FileName, [IO.NotifyFilters]::LastWrite
- # NotifyFilters.Attributes
- # NotifyFilters.CreationTime
- # NotifyFilters.DirectoryName
- # NotifyFilters.FileName
- # NotifyFilters.LastAccess
- # NotifyFilters.LastWrite
- # NotifyFilters.Security
- # NotifyFilters.Size;
- $ChangeTypes = [System.IO.WatcherChangeTypes]::Created #, [System.IO.WatcherChangeTypes]::Deleted, ...
- # YOU CAN ALSO USE THESE FILTERS
- # $ChangeTypes = [System.IO.WatcherChangeTypes]::All
- # $ChangeTypes = [System.IO.WatcherChangeTypes]::Created
- # $ChangeTypes = [System.IO.WatcherChangeTypes]::Changed
- # $ChangeTypes = [System.IO.WatcherChangeTypes]::Renamed
- # $ChangeTypes = [System.IO.WatcherChangeTypes]::Deleted # NOT WORKS WITH THE ATTACHMENT
- $Timeout = 1000 # MILLISECONDS [1 SEC]
- function SendToEmail([string]$email, [string]$attachmentpath){
- $message = new-object Net.Mail.MailMessage;
- $message.From = $Username
- $message.To.Add($email);
- $message.Subject = "Folder Changes";
- $message.Body = "There has been a change in the monitorized folder.";
- $attachment = New-Object Net.Mail.Attachment($attachmentpath);
- $message.Attachments.Add($attachment);
- $smtp = new-object Net.Mail.SmtpClient("smtp.office365.com", "25");
- $smtp.EnableSSL = $true;
- $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
- $smtp.send($message);
- write-host "Mail Sent to $email" ;
- $attachment.Dispose();
- }
- function FileMonitoring
- {
- param
- (
- [Parameter(Mandatory)]
- [System.IO.WaitForChangedResult]
- $ChangeInformation
- )
- Write-Host 'Change detected:'
- $ChangeInformation | Out-String | Write-Host -ForegroundColor DarkYellow
- $FullFilePath = $Path
- $FullFilePath += '\'
- $FullFilePath += $ChangeInformation.Name
- Write-Host $FullFilePath
- ##########################################################################################################
- # CHANGE THIS PART OF THE CODE TO THE DESTINATION EMAILS ##
- ##########################################################################################################
- ##
- #SendToEmail -email "[email protected]" -attachmentpath $FullFilePath; ##
- #SendToEmail -email "[email protected]" -attachmentpath $FullFilePath; ##
- ##
- ##########################################################################################################
- }
- try
- {
- Write-Host "FileSystemWatcher is monitoring $Path"
- $watcher = New-Object -TypeName IO.FileSystemWatcher -ArgumentList $Path, $FileFilter -Property @{
- IncludeSubdirectories = $IncludeSubfolders
- NotifyFilter = $AttributeFilter
- }
- do
- {
- $result = $watcher.WaitForChanged($ChangeTypes, $Timeout)
- if ($result.TimedOut) { continue }
- FileMonitoring -Change $result
- } while ($true)
- }
- finally
- {
- $watcher.Dispose()
- Write-Host 'FileSystemWatcher removed.'
- }
- # YOU CAN CLOSE THE SCRIPT WITH CTRL-C
- # ENJOY!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement