Can't get 'switch' statement to work properly

I’ve got a messy Exchange 2007 powershell script I’m trying to clean up and convert to Exchange 2013. Among one of the messes is a long set of if/elseif commands I’m trying to move to ‘switch’.

$databasename=(Get-MailboxDatabase | foreach {Get-ChildItem ("\" + $.Server + “” + $.edbFilePath).Replace(":","$")| Where-Object{$.Name -ne “Separated.edb” -and $.Name -ne “Journal.edb”}} | sort -property length)[0].name.TrimEnd(".edb")

“MDB1” {$servername=“”}
“MDB2” {$servername=“”}
“MDB3” {$servername=“”}
“MDB4” {$servername=“”}
“MDB5” {$servername=“”}
“MDB6” {$servername=“”}
“MDB7” {$servername=“”}

default {$servername="Something went wrong"}


But for some reason it doesn’t work. I have to be missing something simple. What is it?


Invalid entry

Support question.

You can’t use a method like that in your switch statement. However, the switch statement does support -Wildcard and -Regex options, both of which would work here:

switch -Wildcard ($databasename) {
    "MDB1*" {$servername=""}
    "MDB2*" {$servername=""}
    "MDB3*" {$servername=""}
    "MDB4*" {$servername=""}
    "MDB5*" {$servername=""}
    "MDB6*" {$servername=""}
    "MDB7*" {$servername=""}

    default {$servername="Something went wrong"}

That did it. Thanks!