Search code examples
windowsdockerscheduled-taskstaskschedulerwindows-task-scheduler

Output not redirected to file inside of a windows task


I should also state I'm running this inside a Windows container.

I'm trying to get the output of aws.cmd in a file, but when run in a task it's not sending any output - I can't figure out why.

If I create the following script:

'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

Then create a task:

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

The task runs the script and write this to a file:

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

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



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

Yet when running from the console directly it write output to a file:

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

What's going on here? I sandwich the aws command between two get-date commands which both run so the aws command must be executing without error because it runs BOTH get-date commands.

EDIT: now I'm really confused, if I replace the aws command with nonsense it still runs both get-date commands and I see their output in the log file. For example replacing the aws command with "blah.exe --something" in the script and executing the task it still write both get-date output to the file with no errors. Why?! the script should at least fail at that wrong command! I don't understand what is happening.

EDIT2 (redirecting all output):

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

Redirecting all output I see this error:

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'

Solution

  •  pip install awscli --upgrade --user
    

    I installed with the --user switch, removing that installed it to program files and it was accessible from the task as the system user