SOLVED

How to use multiple tests in a switch statement

Brass Contributor

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
                }
            
            }
        }

 

 

 

2 Replies
best response confirmed by robmo (Brass Contributor)
Solution
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...
Hi Vasil,
Your suggestion was able to get my code working. Thank you for taking the time to look at this!
Rob
1 best response

Accepted Solutions
best response confirmed by robmo (Brass Contributor)
Solution
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...

View solution in original post