Reputation: 53
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?
Upvotes: 5
Views: 16729
Reputation: 59
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: 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: Powershell send-mailmessage - email to multiple recipients
$recipients = "<[email protected]>", "<[email protected]>"
You can also use email distribution groups.
Upvotes: 0
Reputation: 3452
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.
Upvotes: 5
Reputation: 1716
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
Upvotes: 2