Author Posts

May 11, 2015 at 8:12 am

I have a powershell script that listens for a file to be placed in a directory. For this, I am using FileSystemWatcher.

$watcher = New-Object System.IO.FileSystemWatcher -Property @{Path = 'C:\test';
Filter = 'FUBAR.txt';
NotifyFilter = [System.IO.NotifyFilters]'FileName,LastWrite'}

Register-ObjectEvent -InputObject $watcher -EventName Created -SourceIdentifier FileCreatedTest -Action $CreatedAction

Script works great but I need it to essentially constantly run and listen. This means when the box is bounced for patches or whatnot, this script needs to be started automatically. For this piece, I am using new-jobtrigger and register-scheduledjob at startup. This also works, to a point. I placed a debug statement and it writes output to a file when the box is bounced so this tells me the trigger is working as expected. However the piece that is failing is the "action" code that is supposed to run when the file is placed in the directory. It never executes though the condition is being met. Any ideas on what is incorrect here or how to make this work as desired??

May 13, 2015 at 7:18 am

Anyone??? Someone has to have some words of wisdom for this..

May 13, 2015 at 8:37 am

What is $CreatedAction? You are talking about the server being rebooted and restarting the script, so I'm trying to understand the relevance. If you launch the script manually, then the $CreatedAction works, but if your startup job launches it then it doesn't work? If that is the case then the question is what context does the job run in? Do you have run with highest privileges enabled in the job? Are your running the job as SYSTEM and does system have access to do $CreatedAction? For instance, you are writing to a network share or something, then the computer account would have to have permissions to do that.

May 14, 2015 at 12:21 pm

$CreatedAction is essentially a function that processes the file, puts output to a log, and a few other fairly basic things. Its within the script file.

"If you launch the script manually, then the $CreatedAction works, but if your startup job launches it then it doesn't work?"

This is correct. Its a bit bizarre to me.

Do you have run with highest privileges enabled in the job?

Yes.

Are your running the job as SYSTEM

Yes

and does system have access to do $CreatedAction?

I had not considered this and do not know the answer. $CreatedAction exists within the script that is running and I took for granted that in fact is was accessible by whatever needed it. I am not at all sure how to check access to this as I am not sure what is accessing it.

Small snippit:

"PowerShell Script run for date: $($now_date) " | Out-File -FilePath $powershell_log_file -Append

# This block of code runs when the event is heard. IE – a file placed in the directory
$CreatedAction =
{

"In created action function " | Out-File -FilePath $powershell_log_file -Append

## many more lines of code not shown.
......
}

This is code cut and pasted. First line is output to the log file on triggered start. The line within the $CreatedAction code block does NOT output to the log file, when a triggered start is used. When run manually that line is output and the rest of the code block (not shown) executes. Somehow, whatever mechanism that runs the $CreatedAction code block is not running it.

Hopefully this is more clear.

May 14, 2015 at 12:45 pm

It's tough to troublshoot with little snippets of code. The reference to $powershell_log_file, for instance, could be pointing to a network share that SYSTEM (the computer account) does not have access to. Another troubleshooting step, kill the watcher or script, change the context of the startup job to run as you and restart the server (or manually run the task). Does the script work? If so, my guess is there is something in your $CreatedAction block that SYSTEM can't do. You could add a line to create another log locally somewhere place it at the beginning of your $CreatedAction block to see if it's actually triggering.

May 15, 2015 at 8:59 am

Thanks for the info. The log file is in fact local on a D Drive and I had added the line in the $CreatedAction block for exactly the reason you said. I will try and change the context around though and see if something happens. Thanks. This issue is perplexing.