【问题标题】:Redirecting output to $null in PowerShell, but ensuring the variable remains set在 PowerShell 中将输出重定向到 $null,但确保变量保持设置
【发布时间】:2011-05-04 09:16:53
【问题描述】:

我有一些代码:

$foo = someFunction

这会输出一条我想重定向到 $null 的警告消息:

$foo = someFunction > $null

问题是,当我这样做时,虽然成功地抑制了警告消息,但它也有负面的副作用,即没有用函数的结果填充 $foo。

如何将警告重定向到 $null,但仍保留 $foo 填充?

另外,如何将标准输出和标准错误都重定向到 null? (在 Linux 中,它是 2>&1。)

【问题讨论】:

  • 什么会产生警告信息?如果你是someFunction的作者,可以适当修改一下。
  • 实际上,在 Bourne Shell (Linux) 中,它是 2>/dev/null 1>/dev/null;您显示的重定向会将 stderr 重定向到与 stdout 相同的位置——可能是 /dev/null,也可能是常规文件。

标签: powershell


【解决方案1】:

我更喜欢这种方式来重定向标准输出(本机 PowerShell)...

($foo = someFunction) | out-null

但这也有效:

($foo = someFunction) > $null

要在定义 $foo 并返回 "someFunction" 结果后重定向标准错误,请执行以下操作

($foo = someFunction) 2> $null

这实际上与上面提到的相同。

或者从“someFunction”重定向任何标准错误消息,然后用结果定义 $foo:

$foo = (someFunction 2> $null)

要同时重定向,您有几个选择:

2>&1>$null
2>&1 | out-null

【讨论】:

  • 在我将语句包装在 {花括号} 而不是(括号)中之后,此解决方案对我有用。我可能正在使用更新的 PS 版本。
  • 如果我们正在创建后台作业,我们需要完成作业本身:{ myCommandWithAnyOutput & } | Out-Null
【解决方案2】:

这应该可行。

 $foo = someFunction 2>$null

【讨论】:

    【解决方案3】:

    如果你想隐藏错误,你可以这样做

    $ErrorActionPreference = "SilentlyContinue"; #This will hide errors
    $someObject.SomeFunction();
    $ErrorActionPreference = "Continue"; #Turning errors back on
    

    【讨论】:

      【解决方案4】:

      应使用Write-Warning cmdlet 编写警告消息,这允许使用-WarningAction 参数或$WarningPreference 自动变量抑制警告消息。一个函数需要使用CmdletBinding来实现这个功能。

      function WarningTest {
          [CmdletBinding()]
          param($n)
      
          Write-Warning "This is a warning message for: $n."
          "Parameter n = $n"
      }
      
      $a = WarningTest 'test one' -WarningAction SilentlyContinue
      
      # To turn off warnings for multiple commads,
      # use the WarningPreference variable
      $WarningPreference = 'SilentlyContinue'
      $b = WarningTest 'test two'
      $c = WarningTest 'test three'
      # Turn messages back on.
      $WarningPreference = 'Continue'
      $c = WarningTest 'test four'
      

      要在命令提示符处缩短它,您可以使用-wa 0

      PS> WarningTest 'parameter alias test' -wa 0
      

      Write-Error、Write-Verbose 和 Write-Debug 为其相应类型的消息提供类似的功能。

      【讨论】:

        【解决方案5】:

        使用函数:

        function run_command ($command)
        {
            invoke-expression "$command *>$null"
            return $_
        }
        
        if (!(run_command "dir *.txt"))
        {
            if (!(run_command "dir *.doc"))
            {
                run_command "dir *.*"
            }
        }
        

        或者如果你喜欢单行:

        function run_command ($command) { invoke-expression "$command  "|out-null; return $_ }
        
        if (!(run_command "dir *.txt")) { if (!(run_command "dir *.doc")) { run_command "dir *.*" } }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-10-13
          • 2015-09-05
          • 2011-11-10
          • 2013-09-09
          • 2017-01-31
          • 2019-01-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多