What's wrong with my switching function

I'm trying to write a script, part of which is similar to below:

function FooBarMeh ($in,$1,$2)
{
    switch($in)
    {
        $1{'FOO'}
        $2{'BAR'}
        default{'MEH'}
    }
}
$a='A'
$b='B'
$c=read-host
FooBarMeh ($c,$a,$b)

I expect the script to return FOO if I enter A, BAR if I enter B, and MEH if I enter anything else.

However, this is what actually happens:

No matter what I input, I get MEH returned to me three times. If I was to expect MEH at all - even if my first two switching options were somehow broken - I'd think I should only see it once. Why am I getting MEH at all, let alone three times?

Using PowerShell 4.0 on Windows 7 Ultimate

Answers


You don't use parentheses when you call functions in PowerShell. You should call FooBarMeh like this:

FooBarMeh $c $a $b

If you call it with ($c, $a, $b), then you are passing an array of 3 elements. That array gets assigned to $in.

(It's interesting that the switch acts like a loop. It gets executed for each item in the array. I wasn't expecting that.)

Note however when you call .NET functions you do use parentheses. For example:

$a.Contains('A')

Need Your Help

Unable to install postgres 9.1 .. rpm into redhat 6.3

postgresql-9.1 redhat rpm

When I try to install the RPM I get the following error:

PetaPoco in WinForms

c# winforms orm petapoco

How to commit changes done in DataGridView using PetaPoco ? Something like :