简体   繁体   English

如何在 powershell 中使用 Write-Host 将 output 和结果都获取到变量?

[英]How to get both the output and results to a variable using Write-Host in powershell?

I just came across a funny behavior when using Write-Host on powershell.我刚刚在 powershell 上使用Write-Host时遇到了一个有趣的行为。

What I wanted to do is get the colored output from Write-Host and concurrently save the result in a variable.我想要做的是从Write-Host获取彩色 output 并将结果同时保存在一个变量中。 Checking other SO questions, led me to try the following:检查其他 SO 问题,让我尝试以下操作:

$zz = &{
   Write-Warning "hello"
   Write-Error "hello"
   Write-Output "hi"
} 3>&1 2>&1

$zz 
#WARNING: hello
#Write-Error: hello
#hi

$zz = &{ Write-Host -ForegroundColor Red "[ERROR]: Package id: ""jaja""  not found!"; } 3>&1 2>&1
# [ERROR]: Package id: "jaja"  not found!

$zz
# [nothing]

在此处输入图像描述

The output was surprising, and I could not find a way to have the output saved into a variable while also see it displayed, unlike when using the other Write-xxx commandlets. output 令人惊讶,我无法找到一种方法将 output 保存到变量中,同时还能看到它的显示,这与使用其他Write-xxx commandlet 时不同。

Q: What's gong on and how can I get both the output shown and save the results into a variable?问:发生了什么事,我怎样才能同时显示 output 并将结果保存到变量中?


REFERENCES:


UPDATE-1更新-1

Thanks to mklement0 's updated answer , I also tried the following, which almost does what I want, but does not produce the color .感谢mklement0的更新答案,我还尝试了以下方法,它几乎可以满足我的要求,但不会产生颜色

Write-Host -ForegroundColor Red "[ERROR]: Package id: ""jaja""  not found!" 6>&1 | Tee-Object -Variable zz

($zz = Write-Host -ForegroundColor Red "[ERROR]: Package id: ""$package""  not found!" 6>&1)

在此处输入图像描述

The conclusion seem to be that any coloring information is lost when using anything that has to do with redirecting output from Write-Host .结论似乎是,在使用与从Write-Host重定向 output 有关的任何内容时,任何着色信息都会丢失。


UPDATE-2更新 2

Interestingly, the color information are still "there" somewhere.有趣的是,颜色信息仍然“存在”于某处。 Following mklement0's suggestion, I tried to save the color info for 2 different lines.按照 mklement0 的建议,我尝试保存 2 条不同行的颜色信息。 But then the parsing is not correct, as shown below.但是然后解析不正确,如下图。

So with:所以:

$captured = &{ Write-Host -ForegroundColor Red -NoNewline "[ERROR]: some error! " 6>&1; Write-Host -ForegroundColor Green "OKAY"  6>&1 }

We get:我们得到:

在此处输入图像描述

As explained in the answer you link to , you need redirection 6>&1 in order to capture Write-Host output (only works in PowerShell v5 and above):正如您链接到的答案中所解释的,您需要重定向6>&1才能捕获Write-Host output (仅适用于 PowerShell v5 及更高版本):

  • Write-Host output captured via 6>&1 consists of one or more System.Management.Automation.InformationRecord instances, which print as if they were strings, namely by their .MessageData.Message property value, which is the string content of the argument(s) passed to Write-Host .通过6>&1捕获Write-Host output 由一个或多个System.Management.Automation.InformationRecord实例组成,它们就像字符串一样打印,即通过它们的.MessageData.Message属性值,这是参数的字符串内容( s) 传递给Write-Host

  • Therefore, any coloring that stems from the use of the -ForegroundColor and -BackgroundColor parameters is not (directly) passed through:因此,任何源于使用-ForegroundColor-BackgroundColor参数的着色不会(直接)通过:

    • However, the information is preserved, namely in the .MessageData.ForegroundColor and .MessageData.BackgroundColor properties, along with the information about whether -NoNewLine was passed to Write-Host , in Boolean property .MessageData.NoNewLine但是,信息保留,即在.MessageData.ForegroundColor.MessageData.BackgroundColor属性中,以及有关-NoNewLine是否传递给Write-Host的信息,在 Boolean 属性.MessageData.NoNewLine
  • By contrast, coloring via ANSI / VT escape sequences embedded in the original string argument(s) is preserved.相比之下,保留了通过嵌入在原始字符串参数中的ANSI / VT 转义序列进行着色。

Thus, you can recreate the original coloring as follows - note that Write-Host is again used:因此,您可以按如下方式重新创建原始着色- 注意再次使用了Write-Host

$captured = Write-Host -ForegroundColor Red "[ERROR]: some error" 6>&1

$captured | ForEach-Object {
  $messageData = $_.MessageData
  $colorArgs = @{}
  if (-1 -ne $messageData.ForegroundColor) { $colorArgs.ForegroundColor = $messageData.ForegroundColor }
  if (-1 -ne $messageData.BackgroundColor) { $colorArgs.BackgroundColor = $messageData.BackgroundColor }
  Write-Host -Object $captured @colorArgs -NoNewline:$messageData.NoNewLine
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM