【发布时间】:2019-07-18 08:52:28
【问题描述】:
我写了一个脚本,它可以在软件安装与否时返回给你。
我想给它一些颜色,但我不知道如何在与-NoNewline 相同的行上连接 2 个 Write-Output 仅适用于 Write-Host... 在这种情况下我不能使用:
# The function we use to give color
function Positive {
process { Write-Host $_ -ForegroundColor Green }
}
function Negative {
process { Write-Host $_ -ForegroundColor Red }
}
# Is the software installed?
# '0' = NO, is not installed
# '1' = YES, is installed
$Check = '1'
function Check_Installation($Check){
if ($Check -eq '0') {return $response = "No, is not installed" | Negative}
elseif ($Check -eq '1') {return $response = "Yes, is installed" | Positive}
}
$First_Phrase = "Let's check if the software is installed: "
Write-Output "$First_Phrase", "$response"
Check_Installation($Check)
我知道我可以与
连接[string]::Concat("$First_Phrase", "$response")
但不工作。
【问题讨论】:
-
如果你只是想在屏幕上写点东西,你应该使用
Write-Host。特别是如果你想用另一种颜色。 -
谢谢@Olaf,但如果您使用
function,这不是它的工作方式,因为Write-Host只会在终端中写入而不遵守步骤 -
但是
Write-Host是拥有彩色文本的唯一方法。 -
如果您在 Google 上搜索
powershell write-output colors,您会发现许多示例说明了这是如何实现的。但由于function,它们都不适用于我的情况。这就是我在 StackOverflow 上问的原因
标签: powershell scripting concatenation string-concatenation write-host