Reputation: 838
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
Upvotes: 2
Views: 66
Reputation: 16909
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')
Upvotes: 5