1

i am trying to write a function in Powershell that prints a Header in the console with write-output if I run the function with "output" as the first parameter, with write-warning (if the parameter is "warning"), or with write-error (if the parameter is "error").

I can't understand, why $line2 is not printed as it should be in the switch-part. In line 4 it works as expected.

function writeLabel{
    $labelType = $args[0]
    $labelText = $args[1] #    <----

    Write-Output $labelText
    Write-Output $labelType #    <---- this works as expected

    $line1 = "`n-------------------------"
    $line2 = $labelText #    <---- this is not working
    $line3 = "`n-------------------------"

    switch ($labelType){

        warning { 
            Write-Warning $line1
            Write-Warning $line2
            Write-Warning $line3
        }
        error { 
            Write-Error $line1
            Write-Error $line2
            Write-Error $line3
        }  
        output { 
            Write-Output $line1
            Write-Output $line2
            Write-Output $line3
        }  
    }
}

writeLabel("output","Testtext")

Every bit of help is very welcome!

KHa
  • 23
  • 5

1 Answers1

0

You need to call your function without parentheses, like:

writeLabel "output" "Testtext"

Otherwise, PowerShell treat ("output","Testtext") as a single parameter.

On a side node, your function should be called Write-Label

Andrey Marchuk
  • 13,301
  • 2
  • 36
  • 52