PowerShell - "Ternary" If 语句后大写

PowerShell - Uppercase after a "Ternary" If Statement

在 PowerShell 中执行此操作的正确方法是什么?

$action = if ($args.Length > 0) { $args[0] } else { Read-Host 'Action' } #.ToUpper()
echo $action

下面好像有点代码味

$action = if ($args.Length > 0) { $args[0] } else { Read-Host 'Action' }
$action = $action.ToUpper()
echo $action

您拥有的第一个代码块几乎可以像编写的那样工作(您可以分配 if/else 语句的结果)。

$action = $(if ($args.Length -gt 0) { $args[0] } else { Read-Host 'Action' }).ToUpper()

您只需使用 -gt(大于)运算符代替 >,并将其括在括号中。