首页 > 解决方案 > 脚本在 ISE 中工作,但在 Visual Studio Code 中不工作,脚本或 VS Code 有问题?

问题描述

我目前正在运行我为练习而制作的以下脚本,因为我正在尝试提高我的 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 的一些功能,所以我有点恼火。万分感谢!

标签: powershellvisual-studio-codepowershell-ise

解决方案


使用Terminal: Run Selected Text In Active Terminal命令面板中的命令(也可Run Selected Text从菜单访问),单独Terminal提交和执行选择的行,这不适用于函数定义等多行代码段。

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


推荐阅读