我正在运行我为练习而制作的以下脚本,因为我正在尝试提高我的powershell技能,我想从一些基础知识开始,以防我错过了一些东西,因为我慢慢拼凑了我的知识并计算了圈子和其他使用函数的形状/形式。
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中,在突出显示整个脚本并使用“在活动终端中运行所选文本”的热键运行它之后,我得到以下内容。
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
命令,也可以从Run Selected Text
菜单中以Terminal
访问,单独提交和执行选择的行,这不适用于多行片段,如函数定义。
相反,请使用PowerShell扩展附带的PowerShell: Run Selection
命令,该命令默认绑定到热键F8,就像在ISE中一样;您还可以使用Run Selection
命令从选择的上下文菜单中访问它。