Link to home
Start Free TrialLog in
Avatar of tonelm54
tonelm54

asked on

Dont Exit

Im trying to watch a log folder, and when the log folder is changed to email me with the log file as an attachment, so googling about someone has done 99.9% of the work already:-
stackoverflow.com/questions/22294395/script-to-send-new-files-via-email-from-monitored-folder

So modifying it to work for me:-
$watchPath = "C:\Test Folder (3)\"

$sendToList = @("myemail@domain.com")
$sendFrom = "folderChange@domain.co.uk"
$emailSubject = "File '{0}' changed"
$smtpHost = "192.168.1.23"

$watcher = New-Object System.IO.FileSystemWatcher 
$watcher.Path = $watchPath
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true

$changed = Register-ObjectEvent $watcher "Changed" -Action { 

    Send-MailMessage -SmtpServer $smtpHost -To $sendToList -from $sendFrom -Subject ($emailSubject -f $eventArgs.Name) -Attachments $eventArgs.FullPath

} 

Open in new window


Code seems to run, but exits after completing. Is there a way to stop the script from exiting so the email event will work?

Thank you in advance
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial