3

I have scheduled two powershell scripts as tasks to run at 00:00.

This morning I checked the event log and found that one of the scripts failed with the following exception:

Failure. The error was: 'Failed to create log entry in: ‘C:\Users\SPSETU~1\AppData\Local\Temp\PowerShellLog.txt’. The error was: ‘The process cannot access the file 'C:\Users\SPsetupAdmin\AppData\Local\Temp\PowerShellLog.txt' because it is being used by another process.’.'.

  • Can be the problem related to logs? Both the scripts are using the write-log function (see poshcode.org) and log in the windows event log with the same id.
  • Do you know any known conflict among powershell scripts as scheduled tasks?
  • Do I have to execute tasks one a time?

2 Answers 2

4

Well, since you simultaneously try to write to the same file from two different processes you can expect the above error.

Powershell has the same restrictions as any application or program; in this case file write-locks. I don't think there are any "special" restrictions on Powershell scripts as scheduled tasks.

I would either see to that the commands you want to execute uses unique log files (if running them simultaneously is your top priority), or put the commands in the same script and execute this as a scheduled task (if getting everything in one log is your top priority).

2
  • I didn't set the log file, I left the default one. Do you think I can solve the problem using a different event source ID? Apr 5, 2011 at 8:16
  • I will use unique log files, then; it's a good practice anyway. Apr 5, 2011 at 10:18
2

The poshcode.org code logs to the file PowerShellLog.txt as well as the event-log so both of your scripts are trying to write to the same file. If you only want to write to the event-log you could try removing:

if ($Clobber) {
$msg | Out-File -FilePath $Path -Force
} else {
$msg | Out-File -FilePath $Path -Append
}

If you want to keep the file logging then I suggest the introduction of a file name as an argument.

2
  • Thanks, may be is better to add an extra option to disable the file logging. I'll use different log files anyway. Apr 5, 2011 at 10:19
  • 1
    No problem, using different log files is what I was saying about introducing an argument to your function. If you make the $path variable an argument when you call your function then the same function can be used for multiple files. Anyway, happy you've got a way forward :)
    – nimizen
    Apr 5, 2011 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.