如何在同一行上连接2个Write-Output?

时间:2019-02-25 00:20:59

标签: powershell scripting concatenation string-concatenation write-host

我写了一个脚本,可以在未安装软件的情况下返回您。

我想给它一些颜色,但我不知道如何将2个Write-Output-NoNewline连接在一起仅适用于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)

enter image description here

我知道我可以与

连接
[string]::Concat("$First_Phrase", "$response")

但不起作用。

1 个答案:

答案 0 :(得分:1)

这仅在控制台中有效,因为更改ISE中的前景色会更改每一行:

# The function we use to give color

function Set-Output {
    param ($colour, $str1, $str2)

    $t = $host.ui.RawUI.ForegroundColor
    $host.ui.RawUI.ForegroundColor = "$colour"

    $text = "$str1 $str2"

    Write-Output "$text"

    $host.ui.RawUI.ForegroundColor = $t

}

# Is the software installed?
# '0' = NO, is not installed
# '1' = YES, is installed

$Check = '1'

$First_Phrase =  "Let's check if the software is installed: "

Switch ($check) {
    ( 0 ) {Set-Output -colour RED -str1 $First_Phrase -str2 "No, is not installed" }
    ( 1 ) {Set-Output -colour GREEN -str1 $First_Phrase -str2  "Yes, is installed" }
}

Check_Installation($Check)

它所做的只是将两个字符串连接起来并更改前景色。

enter image description here