0

我写了一个脚本,如果安装了软件,它可以返回你。

我想给它一些颜色,但我不知道如何将 2 连接到仅适用于...Write-Output的同一行上,在这种情况下我不能使用:-NoNewlineWrite-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")

但不工作。

4

1 回答 1

2

这仅在控制台中有效,因为在 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)

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

在此处输入图像描述

于 2019-02-25T01:43:28.017 回答