Forum Discussion
robmo
Jan 07, 2022Brass Contributor
How to use multiple tests in a switch statement
Hi,
I am working on a script that gets the CanonicalName for a device from a device and splits it up to determine the support region. I cannot figure out how to get multiple tests to work. If $splitCN[1] equals "Acapulco", "Cozumel", "Lazaro Cardenas", "Mexico City", "Pto Progresso", "Veracruz", $SupportRegion should equal Mexico. I have tried several variations but it doesn't work so far. I know this is possible in other languages but I'm not sure on the PowerShell syntax. How do I accomplish this?
"MEXICO.INT" {
switch ($splitCN[1]) {
("Acapulco","Cozumel","Lazaro Cardenas","Mexico City","Pto Progresso","Veracruz") {
$SupportRegion = "Mexico"
break
}
"Mazanillo" {
$SupportRegion = "ZLO"
break
}
"Tuxpan" {
$SupportRegion = "Tuxpan"
break
}
}
}
- You can use script blocks, for example:
switch ("Cozumel") {
{$_ -in @("Acapulco","Cozumel","Lazaro Cardenas","Mexico City","Pto Progresso","Veracruz")} {
"Mexico"
break
}
}
Refer to the documentation for more info/examples: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_switch?view=powershell-7.2
2 Replies
- You can use script blocks, for example:
switch ("Cozumel") {
{$_ -in @("Acapulco","Cozumel","Lazaro Cardenas","Mexico City","Pto Progresso","Veracruz")} {
"Mexico"
break
}
}
Refer to the documentation for more info/examples: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_switch?view=powershell-7.2- robmoBrass ContributorHi Vasil,
Your suggestion was able to get my code working. Thank you for taking the time to look at this!
Rob