脚本在 ISE 中工作但不在 Visual Studio 代码中,脚本或 VS 代码有问题?

Script Working In ISE but not in Visual Studio Code, problem with script or with VS Code?

我目前 运行 以下脚本是我为练习而制作的,因为我正在尝试提高我的 powershell 技能,我想从一些基础知识开始,以防我在慢慢拼凑的过程中遗漏了一些东西结合我的知识,正在计算圆和其他 shapes/forms 来稍微处理函数。

function calculate-circle{ 
$pi = 3.14
[float]$radius = read-host "What is the radius?"

$surface = $radius*$radius*$pi
write-host "The surface area of a circle with radius $radius is $surface"

}

在 Powershell ISE 中,它的执行没有任何缺陷,我能够输入半径并进行计算。

在 VS Code 中,在突出显示整个脚本并 运行 使用 "Run Selected Text In Active Terminal" 的热键后,我得到以下结果。

PS C:\> function calculate-circle{
Missing closing '}' in statement block or type definition.
At line:0 char:0
PS C:\>     $mypi = 3.14
PS C:\>     [float]$radius = read-host "What is the radius?"
What is the radius?:
PS C:\>     $surface = $radius*$radius*$mypi
PS C:\>     write-host "The surface area of a circle with radius $radius is $surface"
The surface area of a circle with radius 0 is 0
PS C:\> }
At line:1 char:1
+ }
+ ~
Unexpected token '}' in expression or statement.

我想不通的是,这是我的代码的问题还是 VS Code 中 Powershell 的一些怪癖?我真的很喜欢 VS Code 的一些功能,所以我有点生气。非常感谢!

使用命令选项板中的 Terminal: Run Selected Text In Active Terminal 命令,也可以从 Terminal 菜单中作为 Run Selected Text 访问,提交并单独执行选择的行 ,不适用于函数定义等多行代码片段。

而是使用 PowerShell 扩展附带的 PowerShell: Run Selection 命令,它默认绑定到热键 F8,就像在 ISE 中一样;您还可以使用 Run Selection 命令从选择的上下文菜单访问它。