I would like to get an Notification when a certain file where changed. It works fine but I can't set the NotifyFilter. "is not recognized as the name of a cmdlet..." The main problem is: That my script goes twice in my function. A change is identified and then I get two times "changed"
function fileChanged ($text) {
Write-Host $text
}
$watcher = New-object System.IO.FileSystemWatcher "C:\Users\test\Desktop\"
$watcher.EnableRaisingEvents = $true
$watcher.Filter="*.txt"
$watcher.NotifyFilter = (System.IO.NotifyFilters.LastWrite)
$changed = Register-ObjectEvent $watcher "Changed" -Action {
$txt = "changed"
filechanged($txt)
}
while($true) {
echo "wartet"
$null = $watcher.WaitForChanged("Changed")
start-sleep -s 2
}
change this in your code for declaring notifyfilter:
$watcher.NotifyFilter = [System.IO.NotifyFilters]::LastWrite
But this not solve the issue, read this:
From the "Troubleshooting FileSystemWatcher Components" section of the VS.NET documentation...
Multiple Created Events Generated for a Single Action You may notice in certain situations that a single creation event generates multiple Created events that are handled by your component. For example, if you use a FileSystemWatcher component to monitor the creation of new files in a directory, and then test it by using Notepad to create a file, you may see two Created events generated even though only a single file was created. This is because Notepad performs multiple file system actions during the writing process. Notepad writes to the disk in batches that create the content of the file and then the file attributes. Other applications may perform in the same manner. Because FileSystemWatcher monitors the operating system activities, all events that these applications fire will be picked up.
Note: Notepad may also cause other interesting event generations. For example, if you use the ChangeEventFilter to specify that you want to watch only for attribute changes, and then you write to a file in the directory you are watching using Notepad, you will raise an event . This is because Notepad updates the Archived attribute for the file during this operation.