简体   繁体   English

是否有写主机并将其保存到变量的简短方法?

[英]Is there a short way to Write-Host and save it to a variable?

I am trying to Write-Host message and save it to a variable in shortest possible way.我正在尝试Write-Host消息并以最短的方式将其保存到变量中。

Currently my code looks like so:目前我的代码看起来像这样:

Write-Host "Branch with name $branch_name already exists!`nNew branch has not been created."
$message = "Branch with name $branch_name already exists!`nNew branch has not been created."

And of course it works.当然它有效。 I made a special function to compress this:我做了一个特殊的函数来压缩它:

function Write-Host-And-Save([string]$message)
{
Write-Host $message
return $message
}

$message = Write-Host-And-Save "Branch with name $branch_name already exists!`nNew branch has not been created."

However it didn't make any output on screen.但是它没有在屏幕上产生任何输出。 What is more I think there must be a better solution than new function to do it.更重要的是,我认为必须有比新功能更好的解决方案来做到这一点。 And I tried to find one.我试图找到一个。 Unsuccessfully.不成功。

Write-Host "Branch with name $branch_name already exists!`nNew branch has not been created." >> $message
Write-Host "Branch with name $branch_name already exists!`nNew branch has not been created." > $message
Write-Host "Branch with name $branch_name already exists!`nNew branch has not been created." -OutVariable $message

Is there any way to short-circuit that script?有什么办法可以使该脚本短路?

On PowerShell 5+, you can achieve the desired behavior by utilizing Write-Host with the common parameter -InformationVariable .在 PowerShell 5+ 上,您可以通过使用带有通用参数-InformationVariable Write-Host来实现所需的行为。 The following example stores the string value in $message .以下示例将字符串值存储在$message

Write-Host "Branch with name $branch_name already exists" -InformationVariable message

Explanation:解释:

Starting with PowerShell 5, Write-Host became a wrapper for Write-Information .从 PowerShell 5 开始, Write-Host成为Write-Information的包装器。 This means Write-Host writes to the information stream.这意味着Write-Host写入信息流。 Given that behavior, you can store its output into a variable using the -InformationVariable Common Parameter .鉴于该行为,您可以使用-InformationVariable Common Parameter将其输出存储到变量中。


Alternatively, you can achieve similar results with Write-Output using the success stream and the common parameter -OutVariable .或者,您可以使用成功流和通用参数-OutVariable使用Write-Output获得类似的结果。

Write-Output "Branch with name $branch_name already exists" -OutVariable message

Typically, I would be in favor of using Write-Output over Write-Host .通常,我会赞成使用Write-Output不是Write-Host It has a more synchronous behavior and uses the success stream, which is what you are intending to use here.它具有更同步的行为并使用成功流,这正是您在这里打算使用的。 Write-Host does provide the ability to easily color your console output though. Write-Host确实提供了轻松为控制台输出着色的能力。

You can use Tee-Object which forwards its input down the pipeline aswell as saving it into a variable (or a file if desired):您可以使用Tee-Object将其输入转发到管道中,并将其保存到变量(或文件,如果需要):

"Some message" | Tee-Object -Variable message | Write-Host

You can also start with Write-Host :您也可以从Write-Host开始:

Write-Host "Some message" 6>&1 | Tee-Object -Variable message

The 6>&1 redirects the information stream (6) where Write-Host writes to (as of Powershell 5.0), to the standard output stream (1). 6>&1Write-Host写入的信息流 (6)(从 Powershell 5.0 开始)重定向到标准输出流 (1)。 You may even use *>&1 to capture all streams.您甚至可以使用*>&1来捕获所有流。

In this case the final output would end up in the regular output stream though, so it doesn't answer your question exactly.在这种情况下,最终输出最终会出现在常规输出流中,因此它不能准确回答您的问题。 It is just an example, how you can use Tee-Object for the general use case of capturing output to a variable while still outputting it to the console (or any cmdlet further down the pipeline).这只是一个示例,说明如何将Tee-Object用于将输出捕获到变量的一般用例,同时仍将其输出到控制台(或管道中的任何 cmdlet)。

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

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