powershell - 脚本在 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 的一些功能,所以我有点恼火。万分感谢!
解决方案
使用Terminal: Run Selected Text In Active Terminal
命令面板中的命令(也可Run Selected Text
从菜单访问),单独Terminal
提交和执行选择的行,这不适用于函数定义等多行代码段。
而是使用PowerShell: Run Selection
PowerShell 扩展附带的命令,该命令F8默认绑定到热键,就像在 ISE 中一样;您还可以使用Run Selection
命令从选择的上下文菜单中访问它。
推荐阅读
- c - C 中的字符串操作导致随机字符串内容
- html - 如何在 Wordpress 主题中删除网格图像下方无法解释的空间/间隙
- python - 为什么 schema_translate_map 不更改架构?
- office-js - 在 Microsoft Office 加载项中将标题设置为 displayDialogAsync() 对话框
- python - 在 Python / Flask / SQLAlchemy 的表单中创建两个数据库记录之间的关系
- python - 如何将标准化颜色条应用于具有迭代线图的图形
- python - 错误:“无法滚动到视图中”,wait_until_clickable 不起作用
- python - 为什么 python isinstance() 对基类传递而对元类传递不传递?
- excel - 选择过滤数据的最后一个单元格
- node.js - 在mongodb的chats数组中查找对象值的所有匹配项