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
**********************
$Rassignments in theswitchblock 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.Defaultin there, still nothing.$Switchas the name of your parameter.