Share via


Script not writing to log/text file when run as a scheduled task

Question

Wednesday, November 2, 2011 1:00 PM

Anyone know why this is? 

If I run my script, it writes to the log file as it should.  If the script runs from the task scheduler, it does not write to the text file.

All replies (8)

Wednesday, November 2, 2011 2:17 PM ✅Answered

my only guess would be access or its writing some place you don’t expect.

did you specify a full path for the log or just the file name?

 

Justin Rich
http://jrich523.wordpress.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Wednesday, November 2, 2011 2:45 PM

Additionally, is the scheduled task running as your account?

If not, the task account may not have permissions to write to the log folder / file.

Karl

My Blog: http://unlockpowershell.wordpress.com
My Book: Windows PowerShell 2.0 Bible
My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})


Wednesday, November 2, 2011 5:07 PM

my only guess would be access or its writing some place you don’t expect.

did you specify a full path for the log or just the file name?

 

Justin Rich
http://jrich523.wordpress.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

I only specified the log file as "log.txt" since it is in the same directory as the script.  That appears to be the problem, but when it runs as a scheduled task, does it not still run from that same directory?


Wednesday, November 2, 2011 5:12 PM

it might be in the system32 folder. I think perhaps that’s default location

if one is not specified. there is a 'start in' option that is generally left

blank.

 

I could be wrong, but I think that’s what it is.

 

 

Justin Rich
http://jrich523.wordpress.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Wednesday, November 2, 2011 5:33 PM

it might be in the system32 folder. I think perhaps that’s default location

if one is not specified. there is a 'start in' option that is generally left

blank.

 

I could be wrong, but I think that’s what it is.

 

 

Justin Rich
http://jrich523.wordpress.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Yea, I found it in there.  I guess I will just have to specify the exact location of the log file, which sort of sucks since the folder/location may be different depending on the usage.


Wednesday, November 2, 2011 5:41 PM | 1 vote

you could just specify the "start in" value in the task.

 

or, you can try out

 

$log = Join-Path (Split-Path $myinvocation.MyCommand.Path) "log.txt"

 

Justin Rich
http://jrich523.wordpress.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Tuesday, July 3, 2018 12:32 PM

I know this is an old thread but Justin's response is still good in 2018 :)

I hadn't specified the full path, just the log name.

When running from a batch or ps1 file it wrote the log files to the same directory as the script. When run from Task Manager it wrote them to the system32 folder. I added the full path and it wrote them there.

Thanks for this solution

Regards

Colin


Tuesday, September 10, 2019 11:12 AM

Try following settings:

Change User or Group -> Select Your domain user

Select: Run whether user is logged on or not

[Check]:  Do not store password...