2

I'm attempting to use a switch statement to control flow in my PowerShell script, but the switch isn't being processed at all. If I run it interactively (highlight + F8 in ISE), it works without a hitch. I validate the argument, and have tested it thoroughly. If(){} statements process properly. Is there some kind of weird bug with switch statements? Even the Default argument isn't being triggered.

[CmdletBinding()]
Param(
    [Parameter(Mandatory=$True)]
    [ValidateSet('Install','Uninstall','Verify','Enable','Disable')]
    [String]$Switch
)

**FUNCTIONS**

[Int]$R = 324
Start-Transcript 'Path\ESD.log' -Append -Force

Switch ($Switch)
{
    "VERIFY"    { $R = Verify;    Break }
    "INSTALL"   { $R = Install;   Break }
    "UNINSTALL" { $R = Uninstall; Break }
    "ENABLE"    { $R = Enable;    Break }
    "DISABLE"   { $R = Disable;   Break }
    Default { Write-Host "Unable to match: $Switch" -BackgroundColor Red }
}

Write-Host "Exiting with: $R"
Stop-Transcript
EXIT $R

Output:

PS Path\EnterpriseSiteDiscovery> .\ESD verify
Transcript started, output file is Path\ESD.log
Exiting with: 324
Transcript stopped, output file is Path\ESD.log

PS Path\EnterpriseSiteDiscovery> $LASTEXITCODE
324

Content of log:

**********************
Windows PowerShell transcript start
Start time: 20170705154435
**SYSTEMINFO**
**********************
Transcript started, output file is Path\ESD.log
Exiting with: 324
**********************
Windows PowerShell transcript end
End time: 20170705154436
**********************
5
  • That code can't work because the $R assignments in the switch block are assigning unquoted strings (unless those are function names, in which case you should "stub in" function outputs for testing purposes). In other words: Update your code to improve your SSCCE. Commented Jul 5, 2017 at 20:12
  • @Bill_Stewart The entire switch statement isn't getting processed at all. If I comment out the conditions and only have Default in there, still nothing. Commented Jul 5, 2017 at 20:17
  • 2
    Don't use $Switch as the name of your parameter. Commented Jul 5, 2017 at 20:22
  • 1
    dscottraynsford.wordpress.com/2015/09/08/… Commented Jul 5, 2017 at 20:23
  • Interesting question that was difficult to google, but $switch as a name seemed suspicious :) Commented Jul 5, 2017 at 20:27

1 Answer 1

2

It turns out when you are inside a Switch construct, the variable $Switch value is redefined (presumably by the switch construct itself) as an empty variable of type System.Collections.IEnumerator. The value is set to $null. This won’t be a problem if you’re not using a variable with the name $Switch. Unfortunately I was because I was working with a set to Virtual Switches so $Switch seemed like a fair choice of variable name.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.