首页 > 解决方案 > 无法从任务计划程序中的 Powershell 命令保存日志文件

问题描述

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

程序/脚本

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

添加参数

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

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

标签: powershelltaskscheduler

解决方案


您没有得到任何输出,因为您将重定向作为参数传递给 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"

推荐阅读