将 Tee-Object 与重定向一起使用
Use Tee-Object with redirecting
我想运行这个命令和redirect所有输出到Windows以及一个日志文件。
powershell "C:\backup\backup.bat *>&1 | tee log.txt"
所以当我 运行 命令时,我可以看到输出并将其保存在文件中,但我收到此错误:
Ampersand not allowed. The & operator is reserved for future use; use "&" to pa
ss ampersand as a string.
At line:1 char:25
+ C:\backup\backup.bat *>& <<<< 1 | tee log.txt
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordEx
ception
+ FullyQualifiedErrorId : AmpersandNotAllowed
要将 .bat 文件执行的输出发送到控制台和文件,请使用:
powershell "& 'C:\backup\backup.bat' *>&1 | Tee-Object -FilePath 'log.txt'"
有个很好的post,PowerShell and external commands done right,里面解释了如何启动外部命令。之后,只需按照您链接的文章应用重定向即可。
在 PowerShell v3 之前不支持 Success
和 Error
(又名 STDOUT
和 STDERR
)以外的流重定向,如@CB。评论中提到。在 PowerShell v2 中,您只能合并错误流:
powershell "C:\backup\backup.bat 2>&1 | tee log.txt"
我想运行这个命令和redirect所有输出到Windows以及一个日志文件。
powershell "C:\backup\backup.bat *>&1 | tee log.txt"
所以当我 运行 命令时,我可以看到输出并将其保存在文件中,但我收到此错误:
Ampersand not allowed. The & operator is reserved for future use; use "&" to pa
ss ampersand as a string.
At line:1 char:25
+ C:\backup\backup.bat *>& <<<< 1 | tee log.txt
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordEx
ception
+ FullyQualifiedErrorId : AmpersandNotAllowed
要将 .bat 文件执行的输出发送到控制台和文件,请使用:
powershell "& 'C:\backup\backup.bat' *>&1 | Tee-Object -FilePath 'log.txt'"
有个很好的post,PowerShell and external commands done right,里面解释了如何启动外部命令。之后,只需按照您链接的文章应用重定向即可。
在 PowerShell v3 之前不支持 Success
和 Error
(又名 STDOUT
和 STDERR
)以外的流重定向,如@CB。评论中提到。在 PowerShell v2 中,您只能合并错误流:
powershell "C:\backup\backup.bat 2>&1 | tee log.txt"