0

I wrote a script that can return you if a software is installed or not.

I would like to give it a bit of colors but I don't know how to concatenate 2 Write-Output on the same line as -NoNewline only works for Write-Host... which in this case I cannot use:

# 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

I know I can concatenate with

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

but is not working.

Francesco Mantovani
  • 10,216
  • 13
  • 73
  • 113
  • 1
    If you just want to write something to the screen you should use `Write-Host`. Especially if you want to have it in another color. – Olaf Feb 25 '19 at 00:34
  • Thank you @Olaf but this is not how it works if you use `function` as Write-Host will just write in the terminal without respecting the steps – Francesco Mantovani Feb 25 '19 at 00:43
  • But `Write-Host`is the only way to have colored text. – Olaf Feb 25 '19 at 00:45
  • If you search for `powershell write-output colors` on Google you will find many examples that shows how this is possible. But they all not apply to my situation because of the `function`. that's why I asked on StackOverflow – Francesco Mantovani Feb 25 '19 at 00:55

1 Answers1

2

This works in console only as changing the foreground colour in ISE changes it for every line:

# 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)

All that it is doing is concatenating the two strings and changing the foreground colour.

enter image description here

Owain Esau
  • 1,876
  • 2
  • 21
  • 34
  • This looks very good. So there is no way to color just `Yes, is installed` and `No, is not installed`? – Francesco Mantovani Feb 25 '19 at 01:45
  • Unfortunately not that i know of, unless you have them on different lines the colour will be the same for each line. ( https://stackoverflow.com/questions/3896258/how-do-i-output-text-without-a-newline-in-powershell ) will give you an idea of how `Write-Output` works. If you must have different colours on the same line you have to use `Write-Host` with the `-nonewline` parameter – Owain Esau Feb 25 '19 at 01:50
  • What is the reason you cannot use write-host by the way? – Owain Esau Feb 25 '19 at 02:01
  • This is the reason that forces me to use `Write-Output` https://stackoverflow.com/questions/54344622/why-my-powershell-script-is-not-respecting-the-steps-order/54345206?noredirect=1#comment95587924_54345206 – Francesco Mantovani Feb 25 '19 at 02:11