ObjectEvent doesnt work with task scheduler

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

July 23rd, 2013 7:26am

someone?
Free Windows Admin Tool Kit Click here and download it now
July 23rd, 2013 8:30am

Hi,

If your script works standalone but not when you try to run it as a scheduled task, then of course you have some problem with the configuration of the scheduled task. I run PowerShell scripts as scheduled tasks all the time with no problems.

Bill

August 23rd, 2013 10:06am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics