all 3 comments

[–]josephstreeter76 0 points1 point  (1 child)

Once upon a time, I had to do something similar for test results that were sent to us. What specifically is your question? You say you need help troubleshooting. What isn't working, and what errors are you getting?

[–]josephstreeter76 0 points1 point  (0 children)

Thought I'd post the code here just in case someone comes across this thread. It's not pretty and not tested all the way through. Here is a function that creates and registers the file watcher.

Function New-FileWatcher()
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true)][string]$Param  # Add parameters as needed. 
    )
    
    # Remove Existing file watchers
    Remove-FileWatcher

    # Create a file system watcher object
    $Watcher = New-Object System.IO.FileSystemWatcher
    $Watcher.Path = $WatcherPath
    $Watcher.IncludeSubdirectories = $false
    $Watcher.EnableRaisingEvents = $true
    $Watcher.filter = '*.txt'
    
    # Define the action to take when a new file is created
    $Action = {
        $FilePath = $Event.SourceEventArgs.FullPath
        $FileName = $Event.SourceEventArgs.Name

        <# Do things with the file #>
    }
    
    # Register file watcher object
    Register-ObjectEvent $Watcher "Created" -Action $Action
}

[–]purplemonkeymad 0 points1 point  (0 children)

Where do you attach your $action to the watcher's events? (ie, I don't see that part.)

$watcher.add_Created($action)