PowerShell中2>&1的含义是什么?

Bla*_*rai 23 powershell

我有以下脚本:

if( $timeout -ne $null )
{
    & $var$timeout 2>&1 > $logDir\$logName
}
else
{
    & $var2>&1 >  $logDir\$logName
}
Run Code Online (Sandbox Code Playgroud)

我很好奇是什么2>&1; 或者,它代表什么.我不知道它叫什么,否则,我会查一查.

Sea*_*ean 24

它将标准错误(2)重定向到与标准输出相同的位置(1)


lat*_*kin 24

文档是你的朋友.从PS> man about_Redirection

The Windows PowerShell redirection operators are as follows.

Operator  Description                Example
--------  ----------------------     ------------------------------
<snip>

2>&1      Sends errors (2) and       Get-Process none, Powershell 2>&1
          success output (1)
          to the success
          output stream.

<snip>

The syntax of the redirection operators is as follows:

   <input> <operator> [<path>\]<file>

If the specified file already exists, the redirection operators that do not
append data (> and n>) overwrite the current contents of the file without
warning. However, if the file is a read-only, hidden, or system file, the
redirection fails. The append redirection operators (>> and n>>) do not
write to a read-only file, but they append content to a system or hidden
file.
Run Code Online (Sandbox Code Playgroud)

  • 您可以使用通配符搜索帮助系统.`Get-Help'*&1*'`指向上述主题. (9认同)
  • 谢谢你的答案,但正如我在帖子中所说,如果我不知道它是什么,我该如何搜索.如果我知道它是重定向,我将能够使用你的答案,而不需要发布. (3认同)