1

This creates a task that works and writes to a file:

schtasks --% /create /tn "test" /sc minute /mo 10 /ru SYSTEM /tr "powershell get-date | out-file -Encoding:ascii c:\log.log"

This does not create a log file and i cant see any errors:

schtasks --% /create /tn "test2" /sc minute /mo 10 /ru SYSTEM /tr "powershell someexe --help | out-file -Encoding:ascii c:\otherlog.log"

The second command isn't writing to the log file, why? Even if it fails it should still be writing to the file. If I run this from the command line it still writes to the file:

PS C:\> powershell doesntexist --help | out-file -Encoding:ascii c:\fail.log
PS C:\> cat .\fail.log
doesntexist : The term 'doesntexist' is not recognized as the name of a cmdlet, function, scrip
or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ doesntexist --help
+ ~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (doesntexist:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

So I cant understand why the first task is writing to a file and the second one is not

JosefZ
  • 1,564
  • 1
  • 10
  • 18
red888
  • 4,183
  • 18
  • 64
  • 111

1 Answers1

3

powershell someexe --help or even powershell someexe.exe --help: If an executable name is used without the prefixed path then it only runs if it is in the environment path. PowerShell does not execute from the current directory without it.

Use absolute path to executable in /tr argument (e.g. powershell c:\myexes\someexe.exe --help).

Avoid using relative paths (e.g. powershell .\someexe.exe --help) because current directory .\ is a bit unclear as start-in directory is not defined precisely and unambiguously.

JosefZ
  • 1,564
  • 1
  • 10
  • 18
  • darn is there a way to import the system path so I dont need to use relative paths in the task? If I added a start-in dir should that work because when I tried that it made no difference – red888 May 22 '18 at 19:35
  • Do you mean `environment` provider? A path from `Get-ChildItem env:`? – JosefZ May 22 '18 at 22:31