Есть еще переключатели? - PullRequest
0 голосов
/ 13 июля 2020

switch в PowerShell похож на менее запутанный оператор if elseif, но else не работает. Что еще я могу сделать?

switch ("test") {
  "1" {"2"}
  "2" {"1"}
} else {"other"}

выдает ошибку

else : The term 'else' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:4 char:3
+ } else {"other"}
+   ~~~~
    + CategoryInfo          : ObjectNotFound: (else:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Ответы [ 2 ]

7 голосов
/ 13 июля 2020

Остальное для переключателей - это опция default:

switch ("test") {
  "1" {"2"}
  "2" {"1"}
  Default {"other"}
}
4 голосов
/ 13 июля 2020

Вы ищете default {...}

Например:

https://adamtheautomator.com/powershell-switch/

$num = Read-Host "Enter a number"
Switch ($num)
{
    1 {'Run Action 1'}
    2 {'Run Action 2'}
    3 {'Run Action 3'}
    default {'No Action'}
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...