mikedopp icon

File-Watcher.ps1

mikedopp | PRO | 10/03/17 10:37:08 PM UTC | 0 ⭐ | 373 👁️ | Never ⏰ | []
PowerShell |

920 B

|

None

|

0 👍

/

0 👎

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "D:\FTP Data\test\IT-test"
    $watcher.Filter = "*.*"
    $watcher.IncludeSubdirectories = $true
    $watcher.EnableRaisingEvents = $true  
 
 
$smtpServer = "mail.com"
$smtpFrom = "email" 
$smtpTo = "email"
$smtpSubject = "Change to Vulcan FTP Folder"
$username = "g"
$password = "pw"
 
### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
$action = { $smtp = New-Object -TypeName "Net.Mail.SmtpClient" -ArgumentList $smtpServer
$smtp.Credentials = New-Object system.net.networkcredential($username, $Password);
$smtpBody = "[$(Get-Date -Format HH:mm:ss)]`New file arrived: From Vulcan"
$smtp.Send($smtpFrom, $smtpTo, $smtpSubject, $smtpBody)
              }    
### DECIDE WHICH EVENTS SHOULD BE WATCHED 
    Register-ObjectEvent $watcher "Created" -Action $action
    while ($true) {sleep 30}

Comments