【问题标题】:From within a function, how to view the result that so far will be returned to the pipeline从函数内部,如何查看到目前为止将返回到管道的结果
【发布时间】:2014-07-31 11:34:45
【问题描述】:

最近我不得不在PowerShell 脚本中添加一个中间变量,否则New-Item 结果将成为函数结果的一部分:

      # Put in local varible, otherwise we will return it as part of the function result
      $Local:logDirectory = New-Item -ItemType directory -Path $Local:logDir
      Write-Host "Created directory '$Local:logDir'"
  }
  return "$Local:logDir\$LogFileName"

在调试时,我希望能够转储(Write-Host 或其他东西)函数已经排队等待函数返回时在管道中结束的当前位。

所以:

在 PowerShell 函数中,如何查看到目前为止将返回到管道的结果

【问题讨论】:

  • 我认为您所要求的(已在函数中输出到管道的项目的视图)不可用。

标签: powershell powershell-2.0 powershell-3.0


【解决方案1】:

使用 PowerGUI(powershell IDE),您将能够插入断点并检查运行时变量。

http://en.community.dell.com/techcenter/powergui/m/

您也可以使用管道连接到Out-Null,即:

New-Item -ItemType directory -Path $Local:logDir | Out-Null

摆脱虚假输出。不幸的是,您可能必须逐行检查您的代码。

【讨论】:

  • 我试过了,但是PowerGUI没有显示函数已经推送到管道的东西。而且还没有$_,因为这不是循环。
  • 注意。您可以使用New-Item -ItemType directory -Path $Local:logDir | Out-Null 来消除虚假输出。 Y用它来消除任何可能可能弄乱你的返回值的东西。
  • 谢谢。这是局部变量的一个很好的选择。但是我仍然如何检测导致添加的语句? (我宁愿不要用 ` | Out-Null` 结束很多行)
  • 很遗憾,您可能不得不一一浏览它们。
  • 请将最后一点添加到答案中,我会接受。
猜你喜欢
  • 2021-09-06
  • 2015-05-29
  • 2017-10-22
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-23
相关资源
最近更新 更多