Как объединить 2 записи-вывода на одной строке? - PullRequest
0 голосов
/ 25 февраля 2019

Я написал скрипт, который может вернуть вас, если установлено программное обеспечение или нет.

Я хотел бы дать ему немного цветов, но я не знаю, как объединить 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 голосов
/ 25 февраля 2019

Это работает только в консоли, так как изменение цвета переднего плана в 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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...