我正在尝试以最短的方式Write-Host
发送消息并将其保存到变量中。
目前我的代码如下所示:
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."
当然它有效。我做了一个特殊的函数来压缩这个:
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."
但是它没有在屏幕上产生任何输出。更重要的是,我认为必须有比新功能更好的解决方案来做到这一点。我试图找到一个。不成功。
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
有什么方法可以使该脚本短路吗?