【问题标题】:Unable to Save Log File from Powershell Command in Task Scheduler无法从任务计划程序中的 Powershell 命令保存日志文件
【发布时间】:2019-06-18 15:25:38
【问题描述】:

我正在尝试从 Task Scheduler 中的 Powershell 任务创建一个日志文件,但是我尝试的任何方法都不起作用。

程序/脚本

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

添加参数

-ExecutionPolicy Unrestricted -File "C:\Users\Boston\script.ps1" > C:\Users\Boston\script.log

这不是将任务的输出保存到文件中。

【问题讨论】:

  • 你是如何运行任务的,拥有什么权限?检查文件的权限是否可用于任务。在您的实际脚本中使用 Start-Transcript 可能会更好:)

标签: powershell taskscheduler


【解决方案1】:

您没有得到任何输出,因为您将重定向作为参数传递给 powershell.exe,因为重定向不是由 cmd 提示符解释的。例如

可执行文件:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

参数列表:

-ExecutionPolicy 
Unrestricted 
-File 
"C:\Users\Boston\script.ps1" 
> 
C:\Users\Boston\script.log

为了记录输出,最好的方法是在脚本中进行。但是,您可以通过将脚本作为 -Command 运行来解决它,然后使用 PowerShell 本身为您重定向输出:

参数:

-ExecutionPolicy Unrestricted -Command "& C:\Users\Boston\script.ps1 > C:\Users\Boston\script.log"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-06
    • 2019-05-02
    • 2014-12-28
    • 2017-09-28
    • 2022-12-14
    • 2016-01-30
    • 1970-01-01
    • 2018-01-12
    相关资源
    最近更新 更多