Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 重定向命令输出_Shell_Powershell_Redirect_Cmd - Fatal编程技术网

Shell 重定向命令输出

Shell 重定向命令输出,shell,powershell,redirect,cmd,Shell,Powershell,Redirect,Cmd,各位, 我需要将命令输出重定向到2个文件,在一个文件重定向标准输出流中, 将stderr流重定向到另一个文件。 是否可以在windows上的cmd、PowerShell中执行此操作?在PowerShell中,您可以使用众所周知的重定向操作符,>,2>,2>,重定向标准输出和错误 首先确保设置: $erroractionpreference.value__=1 $erroractionpreference.value\ux=1 然后使用重定向 示例: ls C:\2>stderror.txt>st

各位, 我需要将命令输出重定向到2个文件,在一个文件重定向标准输出流中, 将stderr流重定向到另一个文件。
是否可以在windows上的cmd、PowerShell中执行此操作?

在PowerShell中,您可以使用众所周知的重定向操作符
>
2>
2>
,重定向标准输出和错误

首先确保设置:

$erroractionpreference.value__=1 $erroractionpreference.value\ux=1 然后使用重定向

示例:

ls C:\2>stderror.txt>stdoutput.txt#在stdoutput.txt上写入输出


ls foo 2>stderror.txt>stdoutput.txt#在stderror.txt上写入输出,除非powershell存在foo

假设您有
my.ps1
如下所示:

"output"
Write-Error "error output"
exit 1
你可以做:

.\my.ps1 2>stderr.txt | Tee-Object -file stdout.txt
您可以在相应的文件中获得stdout和stderr

有关T形对象的详细信息:

有关捕获所有流的详细信息:


谢谢你的回答,这是对的,但我发现不是这样。我正在C#上编写程序,通过“WScript.shell”执行hg(Mercurial),如何将输出重定向到两个文件,stdout和stderr?最初的问题有点不清楚。你在找这个吗?