Hi Guys
I created the script below to monitor a folder. testing it in Powershell ISE it works normally.
When I configure a task in task scheduler, it doenst work. I configured it to run whether user is logged on or not.
this first echo writes in the file, but the code of the action doesnt work when I create a new file in the directory where I am monitoring.
Seeing event viewer I didnt have errors.
Can you help me about how can I do to fix that?
Thanks
$folder = 'C:\teste\'
$filter = '*.*' # <-- set this according to your requirements
$log='C:\teste\log.txt'
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
IncludeSubdirectories = $false # <-- set this according to your requirements
NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
echo "before the action" >> $log
$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
echo "action works" >> $log
$date=get-date
$minute=$date.Minute
if($minute2 -eq $minute){
echo "Script created to executed just 1 time per minute. It wont execute again this time." >> $log
}
else{
$minute2=$minute
echo "Script executed at $date." >> $log
}
}
#Unregister-Event -SourceIdentifier FileCreated