简体   繁体   English

计划的PowerShell任务将不会写入文件

[英]Scheduled PowerShell task won't write to file

I have a PowerShell (PS) script I want to execute every day at 5:00 am I want to run it to run automatically, so I scheduled it via the Task Scheduler (running Windows 7 Ultimate with SP1, 64-bit). 我有一个PowerShell(PS)脚本,我想每天早上5:00执行,我想运行它以使其自动运行,因此我通过Task Scheduler(运行Windows 7 Ultimate SP1,64位)进行了调度。 As a test, I wrote a simple PS script that just appends to a text file with a time stamp. 作为测试,我编写了一个简单的PS脚本,该脚本仅附加到带有时间戳的文本文件中。 It runs--I can see the PowerShell window open and it writes out the PS log--but it doesn't write to the text file. 它正在运行-我可以看到PowerShell窗口打开并且它写出PS日志-但它不会写入文本文件。

The script runs fine from the command-line, and from the PS shell. 该脚本可以从命令行和PS Shell正常运行。 It doesn't work from Task Scheduler, either from the scheduled time or just from the Task Scheduler list and right-clicking and selected Run. 无论是在计划时间还是从Task Scheduler列表中单击鼠标右键并选择“运行”,它都无法从Task Scheduler中运行。 Both times it runs, but doesn't write to the text file. 两次都运行,但不写入文本文件。

In Task Scheduler, I have the "Actions" set to Start a program, PowerShell.exe. 在任务计划程序中,将“操作”设置为启动程序PowerShell.exe。 For the "Add arguments", I have: -NoProfile -ExecutionPolicy Bypass -Command C:\\AccessTask\\ATestScript.ps1 对于“添加参数”,我具有:-NoProfile -ExecutionPolicy绕过-Command C:\\ AccessTask \\ ATestScript.ps1

The script is simplicity itself: 该脚本本身就是简单性:

Start-Transcript -path "C:\Temp\aTestScript.log" -Verbose

write-output "Start";

# just write one line to file
write-output "Logging";
$text = Get-Date
$text >> 'aTestScriptOutput.txt'

write-output $error[0] | fl -force
write-output "Quit";

Stop-Transcript

Is there something special I have to do to allow a scheduled task to write to a text file? 为了使计划任务写入文本文件,我需要做些特殊的事情吗?

Adding the full path for the file was the answer: 答案是添加文件的完整路径:

$text >> 'C:\Temp\aTestScriptOutput.txt'

Specifying a startup directory may have worked as well. 指定启动目录可能也可以。 Thanks for the help! 谢谢您的帮助!

Scheduled Tasks don't run in the directory of your script. 计划任务不在脚本目录中运行。 You need to set that directory in the Scheduled Task editor. 您需要在“计划任务”编辑器中设置该目录。 I believe it is the "Start In" field. 我相信这是“开始于”字段。

If the output file is always in the same place, you can use a full, absolute path. 如果输出文件始终位于同一位置,则可以使用完整的绝对路径。 To improve portability, use a path relative to the script, eg Join-Path -Path $PSScriptRoot -ChildPath 'aTestScriptOutput.txt' . 为了提高可移植性,请使用相对于脚本Join-Path -Path $PSScriptRoot -ChildPath 'aTestScriptOutput.txt' ,例如Join-Path -Path $PSScriptRoot -ChildPath 'aTestScriptOutput.txt'

Last recommendation: if your output file will only contain text, pipe output to Set-Content instead of redirecting with >> . 最后的建议:如果您的输出文件仅包含文本,请将管道输出到Set-Content而不是使用>>重定向。 The >> operator outputs in UTF-16, which can cause grief when you're trying to view it. UTF-16中的>>运算符输出,在您尝试查看时会引起悲伤。

您应该以最高特权(在任务定义中)运行任务,以使其写入文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM