web-dev-qa-db-ja.com

Windowsタスクは失敗しますが、エラーをスローせず、出力を書き込みません

これにより、機能してファイルに書き込むタスクが作成されます。

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

これはログファイルを作成せず、エラーを確認できません。

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

2番目のコマンドがログファイルに書き込んでいないのはなぜですか?失敗しても、ファイルに書き込んでいるはずです。これをコマンドラインから実行すると、ファイルに書き込まれます。

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

したがって、最初のタスクがファイルへの書き込みであり、2番目のタスクがファイルへの書き込みではない理由がわかりません。

1
red888

powershell someexe --helpまたはpowershell someexe.exe --help:実行可能ファイル名がプレフィックス付きパスなしで使用されている場合、環境パス内にある場合にのみ実行されます。 PowerShellは、PowerShellがないと現在のディレクトリから実行されません。

/tr引数で実行可能ファイルへの絶対パスを使用します(例:powershell c:\myexes\someexe.exe --help)。

現在のディレクトリpowershell .\someexe.exe --helpは少し不明確であるため、相対パス(.\など)の使用は避けてください。開始ディレクトリは正確かつ明確に定義されていません。

3
JosefZ