【问题标题】:How to capture error output only in a variable in PowerShell如何仅在 PowerShell 的变量中捕获错误输出
【发布时间】:2015-03-07 19:26:45
【问题描述】:

我想将 PowerShell 命令的标准错误输出存储在一个变量中。我不想将它存储在文件中,也不想包含标准输出,只包含错误输出。

这会重定向到一个名为error.txt的文件:

& $command $params 2> error.txt

这会将 stderr 和 stdout 都重定向到 $output 变量:

$output = & $command $params 2>&1

但我想将错误输出存储在一个变量中(与上面的error.txt文件的内容相同),而不向文件写入任何内容。我该怎么做?

【问题讨论】:

标签: powershell error-handling stream


【解决方案1】:

您可以以稍微不同的方式调用命令,并在 PowerShell 中使用 -ErrorVariable 参数:

Invoke-Expression "$command $params" -ErrorVariable badoutput

$badoutput 现在将包含错误字符串的内容。

【讨论】:

  • 而且,至少在最新版本的 Powershell 中,您可以捕获 stdout 和 stderr 以分隔变量,然后抑制所有输出,以便您可以完全控制输出发生的情况:调用-表达式 "somecommand.exe" -ErrorVariable errOut -OutVariable succOut 2>&1 >$null
  • 对我不起作用; Invoke-Expression 'whoami i' -ErrorVariable badoutput$badoutput 中没有给出任何内容,而我期望 ERROR: Invalid argument/option - 'i'. Type "WHOAMI /?" for usage.
  • 没关系。这只是一些PowerShell mess
  • @ZameerFouzan 1 是标准输出,2 是标准错误,2>&1 将标准错误合并到标准输出中。然后>$null 丢弃标准输出
【解决方案2】:

这是Simon Wahlin 使用子表达式的更简单的解决方案

$output = & $command $params 2>&1

应该是:

$errOutput = $( $output = & $command $params ) 2>&1

【讨论】:

    【解决方案3】:

    要添加到arco444,具体的异常可以通过$badoutput[0].Exception获取。

    -ErrorAction and -ErrorVariable 提供了有关有效使用 PowerShell 中内置的 ErrorAction 和 ErrorVariable 参数的更多信息。

    这是展示此工作的最简单方法:

    PS> Stop-Process 13,23
    Stop-Process : Cannot find a process with the process identifier 13.
    At line:1 char:13
    + Stop-Process  <<<< 13,23
    Stop-Process : Cannot find a process with the process identifier 23.
    At line:1 char:13
    + Stop-Process  <<<< 13,23
    
    PS> Stop-Process 13,23 -ErrorAction Stop  # Only 1 error
    Stop-Process : Command execution stopped because the shell variable “ErrorA
    ctionPreference” is set to Stop: Cannot find a process with the process ide
    ntifier 13.
    At line:1 char:13
    + Stop-Process  <<<< 13,23 -ErrorAction Stop  # Only 1 error
    
    PS> Stop-Process 13,23 -ErrorAction silentlycontinue  # No errors
    
    PS> Stop-Process 13,23 -ErrorAction inquire  # ASK
    Confirm
    Cannot find a process with the process identifier 13.
    [Y] Yes  [A] Yes to All  [H] Halt Command  [S] Suspend  [?] Help
    (default is “Y”):h
    Stop-Process : Command execution stopped because the user selected the Halt
     option.
    At line:1 char:13
    + Stop-Process  <<<< 13,23 -ErrorAction inquire  # ASK
    PS>
    PS>
    PS> Stop-Process 13,23 -ErrorVariable a -ErrorAction SilentlyContinue
    
    PS> $a[0]
    Stop-Process : Cannot find a process with the process identifier 13.
    At line:1 char:13
    + Stop-Process  <<<< 13,23 -ErrorVariable a -ErrorAction SilentlyContinue
    
    PS> $a[0] |fl * -Force
    
    Exception             : Microsoft.PowerShell.Commands.ProcessCommandExcepti
                            on: Cannot find a process with the process identifi
                            er 13.
    TargetObject          : 13
    CategoryInfo          : ObjectNotFound: (13:Int32) [Stop-Process], ProcessC
                            ommandException
    FullyQualifiedErrorId : NoProcessFoundForGivenId,Microsoft.PowerShell.Comma
                            nds.StopProcessCommand
    ErrorDetails          :
    InvocationInfo        : System.Management.Automation.InvocationInfo
    
    PS> $a |ft TargetObject -force -auto
    TargetObject
    ————
              13
              23
    

    现在人们不明白的一件事是,您可以在 ErrorVariable 的变量名前指定一个“+”,我们会将错误添加到该变量中。

    PS> $err=@()
    PS> stop-process 13 -ea silentlycontinue -ErrorVariable err
    PS> $err.count
    1
    
    PS> stop-process 23 -ea silentlycontinue -ErrorVariable +err
    PS> $err.count
    2
    PS> $err
    Stop-Process : Cannot find a process with the process identifier 13.
    At line:1 char:13
    + stop-process  <<<< 13 -ea silentlycontinue -ErrorVariable err
    Stop-Process : Cannot find a process with the process identifier 23.
    At line:1 char:13
    + stop-process  <<<< 23 -ea silentlycontinue -ErrorVariable +err
    

    最后,您无需输入 –ErrorAction 或 –ErrorVariable,我们为它们定义了参数别名,因此您只需输入 –EA-EV

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-10
      • 2020-05-23
      • 1970-01-01
      • 2011-12-27
      • 2012-09-09
      • 1970-01-01
      • 2017-05-27
      相关资源
      最近更新 更多