Tee-Object - 变量不发送到标准输出

Tee-Object -Variable not sending to stdout

我有一段脚本,其中包含以下行:

myBinary.exe --param1 a -param2 b | Tee-Object -Variable "myVar"

它曾经很好地流式传输输出。现在我将这一行移动到一个函数中,它不再流向标准输出并且只填充变量 myVar.

知道如何解决这个问题吗?

我们没有足够的信息来诊断您的问题,但是有一个 Tee-Object 陷阱 可能 解释您的问题症状:

截至 Windows PowerShell v5.1/PowerShell Core v6.0.0-rc:

如果Tee-Object没有通过管道接收到任何成功输出,它会留下目标变量的previous value untouched 或者,如果没有该名称的预先存在的变量, 创建它。

# Run command that produces stdout and therefore PS success-stream output.
cmd /c 'echo hi' | Tee-Object -Variable myVar

# Stdout -> success output was correctly captured in $myVar.
$myVar  # -> 'hi'

# Run another command that *doesn't* produce any stdout output
# (only stderr output).
cmd /c 'echo NO >&2' | Tee-Object -Variable myVar

# !! Perhaps unexpectedly, the previous value of $myVar was NOT cleared.
$myVar # -> !! STILL 'hi'

这种不直观的行为已经 reported on GitHub

繁琐的解决方法是首先将目标变量显式设置为 $null
$myVar = $null


因此,也许您在 $myVar 中看到的是一个 预先存在的 值 - 现在看似非标准输出输出 - myBinary.exe ... | Tee-Object ... 来电不清晰。