I have a Hyper-V test environment on my work PC where I use a small PowerShell function for quickly creating new VMs from exported ones that I use as templates. But when I added a switch
statement to the function in order to enhance template selection I discovered that PowerShell ignores the switch
statement completely.
By stripping down the function I found out that the problem arises from a parameter named $Switch
(for attaching the new VM to a virtual switch).
Demonstration:
function foo {
Param(
[String] $Switch
)
'start'
switch ($true) {
default { 'should display' }
}
'end'
}
Invoking the above function (with or without argument) produces the following output:
start
end
when it should produce:
start
should display
end
Renaming the parameter resolves the issue, so I'm going to do that, but even so I'd like to understand what is going wrong here.