0

I am trying to return a color based on two boolean values. But, It keeps returning White or Gray. None of the red colors.

I have these two functions in a powershell script file:

function GetTextColor($shouldBeHighlighted, $shouldBeError){
    if($shouldBeError -eq $true){
        if($shouldBeHighlighted -eq $true){
            return "Red"
        } else {
            return "DarkRed"
        }
    } else {
        if($shouldBeHighlighted -eq $true){
            return "White"
        } else {
            return "Gray"
        }
    }
}

function PrintColoredText($content, $isError){
    if($line -match "abc") {
        $color = GetTextColor($true, $isError)
        Write-Host $line -ForegroundColor $color
    } else {
        $color = GetTextColor($false, $isError)
        Write-Host $line -ForegroundColor $Color
    }
}

I call the function with something like these:

PrintColoredText("some text", $true)
PrintColoredText("some text", $false)

What am I doing wrong?

7heViking
  • 7,137
  • 11
  • 50
  • 94
  • 1
    Does this answer your question? [How do I pass multiple parameters into a function in PowerShell?](https://stackoverflow.com/questions/4988226/how-do-i-pass-multiple-parameters-into-a-function-in-powershell), in your case to call a function: `PrintColoredText "some text" $true`. See also: [classic PowertShell gotchas (#2)](https://stackoverflow.com/a/69644807/1701026) – iRon Mar 01 '22 at 11:43
  • Yes - the problem was the call of the function which should not have () or , – 7heViking Mar 01 '22 at 11:48
  • 1
    Besides, you might want to use [switch parameters](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters#switch-parameters) instead. – iRon Mar 01 '22 at 12:05

0 Answers0