【问题标题】:Output not redirected to file inside of a windows task输出未重定向到 Windows 任务内的文件
【发布时间】:2018-05-23 18:46:31
【问题描述】:

我还应该说明我在 Windows 容器中运行它。

我试图在文件中获取 aws.cmd 的输出,但是在任务中运行时它没有发送任何输出 - 我不知道为什么。

如果我创建以下脚本:

'get-date | out-file c:\logs\date.txt; C:\users\containeradministrator\appdata\Roaming\Python\Python37\Scripts\aws.cmd --version | out-file -append c:\logs\date.txt; getdate | out-file -append c:\logs\date.txt' > getdate.ps1

然后创建一个任务:

schtasks --% /create /tn "test-date" /sc minute /mo 1 /ru SYSTEM /tr "powershell -file c:/getdate.ps1"

任务运行脚本并将其写入文件:

PS C:\> cat C:\logs\date.txt

Wednesday, May 23, 2018 2:38:00 PM



Wednesday, May 23, 2018 2:38:02 PM

然而,当直接从控制台运行时,它会将输出写入文件:

PS C:\> C:\users\containeradministrator\appdata\Roaming\Python\Python37\Scripts\aws.cmd --version > C:\logs\consoletest.txt
PS C:\> cat C:\logs\consoletest.txt
aws-cli/1.15.24 Python/3.7.0b4 Windows/10 botocore/1.10.24

这是怎么回事我失去了理智。我将 aws 命令夹在两个都运行的 get-date 命令之间,因此 aws 命令必须在没有错误的情况下执行,因为它同时运行两个 get-date 命令。

编辑:好的,现在我真的很困惑,如果我用废话替换 aws 命令,它仍然会运行两个 get-date 命令,并且我会在日志文件中看到它们的输出。例如,在脚本中用“blah.exe --fartfartfart”替换 aws 命令并执行任务,它仍然将 get-date 输出写入文件而没有错误。为什么?!该脚本至少应该在那个错误的命令上失败!我不明白发生了什么

EDIT2(重定向所有输出):

try {
  C:\Users\ContainerAdministrator\AppData\Roaming\Python\Python37\Scripts\aws.cmd --version *>> c:\logs\date.txt
}
catch {
  $_.Exception | out-file c:\logs\date.txt
}
get-date | out-file -apend c:\logs\date.txt

重定向所有输出我看到这个错误:

C:\Users\ContainerAdministrator\AppData\Roaming\Python\Python37\Scripts\aws.cmd
 : Traceback (most recent call last):
At C:\getdate.ps1:1 char:6
+ try {C:\Users\ContainerAdministrator\AppData\Roaming\Python\Python37\ ...
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Traceback (most recent call last)
   ::String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

  File "C:\Users\ContainerAdministrator\AppData\Roaming\Python\Python37\Scripts
\aws.cmd", line 50, in <module>
    import awscli.clidriver
ModuleNotFoundError: No module named 'awscli'

【问题讨论】:

  • PowerShell 继续执行脚本,因为它有一类称为非终止错误的错误。在脚本级别,您可以设置 $ErrorActionPreference = "Stop" 以使其实际停止这些错误。
  • 那是路径,那是 python 安装的地方,那是 whoami,不要认为那是问题,因为它在任务之外运行良好
  • ?我的第二次编辑有什么问题
  • 我用 --user 安装它,因为这就是文档所说的,但在这种情况下,我认为这是导致问题的原因
  • 是的,您正在以 SYSTEM 身份运行任务,它不会拥有您的 ContainerAdministrator 帐户的每个用户 PATH 值或访问每个用户安装的模块。

标签: windows docker scheduled-tasks taskscheduler windows-task-scheduler


【解决方案1】:
 pip install awscli --upgrade --user

我使用 --user 开关安装,将它安装到程序文件中,并且可以作为系统用户从任务中访问

【讨论】:

    猜你喜欢
    • 2020-08-03
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    • 2013-07-12
    • 2011-07-10
    相关资源
    最近更新 更多