Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 811 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Email Notification when file added to folder

#1
I know very little about scripting, but I think what I want is possible. I would simply like to get an email notification when a file is added to a particular folder. I don't have any specific software, so I guess it would have to be a batch file or maybe VBS?
Reply

#2
Do you have information about your email server? What version of Windows and Powershell do you have?

This short Powershell script works in my environment:

$folder = "D:\"
$mailserver = "your.mailserver.your.company"
$recipient = "[email protected]"

$fsw = New-Object System.IO.FileSystemWatcher $folder -Property @{
IncludeSubdirectories = $true
NotifyFilter = [IO.NotifyFilters]'FileName'
}

$created = Register-ObjectEvent $fsw -EventName Created -Action {
$item = Get-Item $eventArgs.FullPath
$s = New-Object System.Security.SecureString
$anon = New-Object System.Management.Automation.PSCredential ("NT AUTHORITY\ANONYMOUS LOGON", $s)
Send-MailMessage -To $recipient `
-From "[email protected]" `
-Subject “File Creation Event” `
-Body "A file was created: $($eventArgs.FullPath)" `
-SmtpServer $mailserver `
-Credential $anon
}

Stop the alerts with this:

Unregister-Event -SourceIdentifier Created -Force
Reply

#3
You can do this, in a batch-file with powershell enabled:

@echo off
setlocal EnableDelayedExpansion
set "[email protected]"
set "emailPassword=dummyPassword"
set "[email protected]"
set "subject=File Changed"
FOR %%G IN (*) DO attrib -A "%%G"
:loop
set "body="
FOR %%G IN (*) DO (
attrib "%%G" | findstr /B /L A 1>nul
if !errorlevel! equ 0 (
echo "%%G"
set "body=!body!^<br ^/^>%%G"
attrib -A "%%G"
)
) 2>nul
if not "%body%"=="" echo sending email
if not "%body%"=="" set "body=The following files have been changed:!body!"
if not "%body%"=="" powershell.exe -command "Send-MailMessage -From '!emailUserName!' -to '!target!' -Subject '!subject!' -Body '!body!' -BodyAsHtml -SmtpServer 'smtp.gmail.com' -port '587' -UseSsl -Credential (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ('!emailUserName!', (ConvertTo-SecureString -String '!emailPassword!' -AsPlainText -Force)))"
goto :loop

For this to work, you need to create a dummy gmail acount that will send the email. This supports HTML tags in the body, as shown in the example.

Note that this doesn't work on deletion of files, only changes and new files.
Reply

#4
I built my solution based on what @xXhRQ8sD2L7Z contributed. But I was getting multiple emails as I tested, a new one for every time I ran the register script. The Unregister line was not working:

Unregister-Event -SourceIdentifier Created -Force

So I found this: <https://stackoverflow.com/questions/33033901/unregister-a-registered-filewatcher-event-does-not-work>

Get-EventSubscriber -SourceIdentifier "filechanged" | Unregister-Event
but it still wasn't working, so I ran `Get-EventSubscriber` by itself and got a list of the Subscriptions for the Event and found that, in my case, the `-SourceIdentifier` was a GUID. With that understanding, I grabbed each individual GUID and proceeded to unregister them. (I could've looped through them, but I will only use this once, probably. Plus, I had only created the subscription 6 times before I realized what was happening)

This eliminated all of the extra emails i received every time I tested.

Get-EventSubscriber -SourceIdentifier "<very long GUID>" | Unregister-Event


Then I wanted to send to multiple recipients, so i found this: <https://stackoverflow.com/questions/10241816/powershell-send-mailmessage-email-to-multiple-recipients>

$recipients = "<[email protected]>", "<[email protected]>"

You can also use email distribution groups.
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through