Visual Studio 代码是否与命令面板中的 PowerShell 一起工作?
Does Visual Studio Code work with PowerShell in the Command Palette?
使用 Visual Studio Code(不是传统的 Visual Studio IDEs),您可以在命令面板中 运行 PowerShell 吗?我问是因为我常用全IDE.
我没有看到 PowerShell 被提及 in the documentation,除了基本的语法高亮显示。我尝试过但没有成功。它是不受支持的,还是我可以以某种方式配置的可选功能?
注意:对于那些投票支持 PowerGUI 答案的人来说,它是不正确的,因为它引用了错误版本的 Visual Studio 这个问题。如果您使用的是完整的 IDE,而不是新的名为代码的编辑器,这将很有帮助。
PowerGUI 工具为 Visual Studio 提供了一个很好的界面。此扩展的目标是将 PowerShell 开发带入 Visual Studio。
这是它的样子 -
与 IntelliSense 一起,PowerGUI Visual Studio 扩展提供了以下功能以及更多功能,可以更轻松地使用 PowerShell。
PowerShell 文件和项目类型: 您可以 create/edit PowerShell 代码文件并将它们 assemble 到具有多个文件的项目中。
PowerShell 代码片段:代码片段功能可用于 PowerShell 代码。
PowerShell 控制台 window: 此功能在 Visual Studio IDE 中提供 PowerShell 控制台环境。这允许您 运行 命令或查看脚本的输出。图 B 显示了在 IDE.
中打开的控制台 window
PowerShell 调试: 这个功能是我安装扩展的原因;它提供了一种在 Visual Studio 中调试脚本的方法。这是在脚本中定位语法或逻辑问题的直接方法。
语法突出显示和脚本分析:这些是更多 Visual Studio 可用于 PowerShell 开发的功能。
为了安装 PowerGUI Visual Studio 扩展,必须安装 PowerGUI,但需要注意的是每个产品都安装了正确的版本。
它还提供调试工具,这是我作为开发人员最喜欢的部分。
谢谢!
这是我想在 VSCode 中解决的第一个问题。我没有找到办法
键入 PowerShell 命令,但我找到了创建和 运行 PowerShell tasks.
的方法
在命令面板中键入并选择任务:配置任务运行程序。它会
显示为默认任务配置的 json 文件 运行ner.
我把内容换成了这个
{
"version": "0.1.0",
"command": "PowerShell.exe",
"isShellCommand": true,
"args": [
"-NoProfile",
"Invoke-Build.ps1"
],
"tasks": [
{
"taskName": "Build",
"isBuildCommand": true,
"showOutput": "always"
},
{
"taskName": "Test",
"isTestCommand": true,
"showOutput": "always"
},
{
"taskName": "Build, Test",
"showOutput": "always"
}
]
}
因此,我可以在命令面板中选择和 运行 我的预定义任务(构建、测试和组合构建、测试)。我可以添加其他任务,并可能将它们绑定到一些热键。这不完全是我想要在 VSCode 中为 PowerShell 提供的,但对于预览来说至少是一些东西。
P.S。这只是我的第一个有点奏效的实验。它并不完美,更有可能。这个json文件有很多配置参数我还没试过。
以下是配置 Powershell 任务以执行当前打开的 .ps1 文件的方法,无需任何 Invoke-Build 依赖项:
{
"version": "0.1.0",
"command": "PowerShell.exe",
"isShellCommand": true,
"args": [
"${file}"
],
"tasks": [
{
"taskName": "Build",
"isBuildCommand": true,
"showOutput": "always"
},
{
"taskName": "Test",
"isTestCommand": true,
"showOutput": "always"
},
{
"taskName": "Build, Test",
"showOutput": "always"
}
]
}
注意:这只是对 Roman 的答案的轻微修改(我对他的答案的编辑被拒绝)。
使用 Visual Studio 代码的 0.10.1 版,您可以 运行 调试 PowerShell。这是我使用的示例 launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "PowerShell",
"type": "PowerShell",
"program": "MyScript.ps1",
"args": "-Verbose"
}
]
}
不幸的是,我无法使 args 起作用(有关详细信息,请参阅:https://github.com/PowerShell/vscode-powershell/issues/24). Another problem I have is that Read-Host does not work with VS Code (for more details, see: https://github.com/PowerShell/vscode-powershell/issues/29)。肯定有些毛边。
从版本 0.10.3 开始,现在有一个由 Microsoft 编写的 PowerShell 扩展,可以使用 IntelliSense 编写 PowerShell 并在 Visual Studio 代码
中突出显示
至少在 V1.4(2016 年 7 月)中,这已经变得简单多了。
相关文档可以在这里找到:
https://code.visualstudio.com/docs/editor/integrated-terminal
基本上您所做的只是在 settings.json 文件中为用户设置添加一个条目。
添加以下值:
// 64-bit PowerShell if available, otherwise 32-bit
"terminal.integrated.shell.windows":"C:\Windows\sysnative\WindowsPowerShell\v1.0\powershell.exe"
IMG:显示用户设置 settings.json 文件和显示 PowerShell 提示的成功集成控制台:
有一种更简单的方法 运行 PowerShell,无需配置:
- 安装 Code Runner Extension
- 在文本编辑器中打开 PowerShell 代码文件,然后使用快捷键
Ctrl+Alt+N
,或按 F1
然后按 select/type Run Code
,或右键单击文本编辑器并然后在上下文菜单中单击 Run Code
,代码将 运行 并且输出将显示在输出 Window. 中
此外,您可以 select 部分 PowerShell 代码和 运行 代码片段。很方便!
打开 Debug 视图,在视图栏中 select Debug 从 View 菜单或按 Ctrl + Shift + D.
在启动配置下拉菜单中(如以下屏幕截图所示),select 添加配置...
launch.json 配置将在编辑器中打开,键入 PowerShell 和 select 您想要的调试配置,如以下屏幕截图所示。
保存 launch.json 文件和 select 所需的 启动配置 下拉菜单中的调试配置:
现在您可以通过VSCode调试PowerShell脚本了。
脚本专家就此主题写了一篇由两部分组成的综合博客 post,就像他们写的其他所有内容一样,如果您是 VSCode 和 PowerShell 的新手,这本书非常值得一读。
编辑:我知道这个问题已经有好几年了,但在我找到任何关于如何在 VSCode
中设置 PowerShell 调试的最新信息之前,我遇到了它
"Integrated Terminal" 现在已集成到 Visual Studio 代码中。这是 Windows 中的 PowerShell 终端。 (在 Linux / MacOS 中可能 BASH)。终端显示在代码编辑器底部的面板中,而不是命令面板中。
打开终端的键盘快捷键:CTRL + `
来源:
- https://code.visualstudio.com/shortcuts/keyboard-shortcuts-windows.pdf
- https://code.visualstudio.com/docs/getstarted/keybindings
我在1.19.2版本确认过。不确定何时首次集成该功能。
使用 Visual Studio Code(不是传统的 Visual Studio IDEs),您可以在命令面板中 运行 PowerShell 吗?我问是因为我常用全IDE.
我没有看到 PowerShell 被提及 in the documentation,除了基本的语法高亮显示。我尝试过但没有成功。它是不受支持的,还是我可以以某种方式配置的可选功能?
注意:对于那些投票支持 PowerGUI 答案的人来说,它是不正确的,因为它引用了错误版本的 Visual Studio 这个问题。如果您使用的是完整的 IDE,而不是新的名为代码的编辑器,这将很有帮助。
PowerGUI 工具为 Visual Studio 提供了一个很好的界面。此扩展的目标是将 PowerShell 开发带入 Visual Studio。
这是它的样子 -
与 IntelliSense 一起,PowerGUI Visual Studio 扩展提供了以下功能以及更多功能,可以更轻松地使用 PowerShell。
PowerShell 文件和项目类型: 您可以 create/edit PowerShell 代码文件并将它们 assemble 到具有多个文件的项目中。 PowerShell 代码片段:代码片段功能可用于 PowerShell 代码。
PowerShell 控制台 window: 此功能在 Visual Studio IDE 中提供 PowerShell 控制台环境。这允许您 运行 命令或查看脚本的输出。图 B 显示了在 IDE.
中打开的控制台 windowPowerShell 调试: 这个功能是我安装扩展的原因;它提供了一种在 Visual Studio 中调试脚本的方法。这是在脚本中定位语法或逻辑问题的直接方法。
语法突出显示和脚本分析:这些是更多 Visual Studio 可用于 PowerShell 开发的功能。
为了安装 PowerGUI Visual Studio 扩展,必须安装 PowerGUI,但需要注意的是每个产品都安装了正确的版本。
它还提供调试工具,这是我作为开发人员最喜欢的部分。
谢谢!
这是我想在 VSCode 中解决的第一个问题。我没有找到办法 键入 PowerShell 命令,但我找到了创建和 运行 PowerShell tasks.
的方法在命令面板中键入并选择任务:配置任务运行程序。它会 显示为默认任务配置的 json 文件 运行ner.
我把内容换成了这个
{
"version": "0.1.0",
"command": "PowerShell.exe",
"isShellCommand": true,
"args": [
"-NoProfile",
"Invoke-Build.ps1"
],
"tasks": [
{
"taskName": "Build",
"isBuildCommand": true,
"showOutput": "always"
},
{
"taskName": "Test",
"isTestCommand": true,
"showOutput": "always"
},
{
"taskName": "Build, Test",
"showOutput": "always"
}
]
}
因此,我可以在命令面板中选择和 运行 我的预定义任务(构建、测试和组合构建、测试)。我可以添加其他任务,并可能将它们绑定到一些热键。这不完全是我想要在 VSCode 中为 PowerShell 提供的,但对于预览来说至少是一些东西。
P.S。这只是我的第一个有点奏效的实验。它并不完美,更有可能。这个json文件有很多配置参数我还没试过。
以下是配置 Powershell 任务以执行当前打开的 .ps1 文件的方法,无需任何 Invoke-Build 依赖项:
{
"version": "0.1.0",
"command": "PowerShell.exe",
"isShellCommand": true,
"args": [
"${file}"
],
"tasks": [
{
"taskName": "Build",
"isBuildCommand": true,
"showOutput": "always"
},
{
"taskName": "Test",
"isTestCommand": true,
"showOutput": "always"
},
{
"taskName": "Build, Test",
"showOutput": "always"
}
]
}
注意:这只是对 Roman 的答案的轻微修改(我对他的答案的编辑被拒绝)。
使用 Visual Studio 代码的 0.10.1 版,您可以 运行 调试 PowerShell。这是我使用的示例 launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "PowerShell",
"type": "PowerShell",
"program": "MyScript.ps1",
"args": "-Verbose"
}
]
}
不幸的是,我无法使 args 起作用(有关详细信息,请参阅:https://github.com/PowerShell/vscode-powershell/issues/24). Another problem I have is that Read-Host does not work with VS Code (for more details, see: https://github.com/PowerShell/vscode-powershell/issues/29)。肯定有些毛边。
从版本 0.10.3 开始,现在有一个由 Microsoft 编写的 PowerShell 扩展,可以使用 IntelliSense 编写 PowerShell 并在 Visual Studio 代码
中突出显示至少在 V1.4(2016 年 7 月)中,这已经变得简单多了。
相关文档可以在这里找到: https://code.visualstudio.com/docs/editor/integrated-terminal
基本上您所做的只是在 settings.json 文件中为用户设置添加一个条目。
添加以下值:
// 64-bit PowerShell if available, otherwise 32-bit
"terminal.integrated.shell.windows":"C:\Windows\sysnative\WindowsPowerShell\v1.0\powershell.exe"
IMG:显示用户设置 settings.json 文件和显示 PowerShell 提示的成功集成控制台:
有一种更简单的方法 运行 PowerShell,无需配置:
- 安装 Code Runner Extension
- 在文本编辑器中打开 PowerShell 代码文件,然后使用快捷键
Ctrl+Alt+N
,或按F1
然后按 select/typeRun Code
,或右键单击文本编辑器并然后在上下文菜单中单击Run Code
,代码将 运行 并且输出将显示在输出 Window. 中
此外,您可以 select 部分 PowerShell 代码和 运行 代码片段。很方便!
打开 Debug 视图,在视图栏中 select Debug 从 View 菜单或按 Ctrl + Shift + D.
在启动配置下拉菜单中(如以下屏幕截图所示),select 添加配置...
launch.json 配置将在编辑器中打开,键入 PowerShell 和 select 您想要的调试配置,如以下屏幕截图所示。
保存 launch.json 文件和 select 所需的 启动配置 下拉菜单中的调试配置:
现在您可以通过VSCode调试PowerShell脚本了。
脚本专家就此主题写了一篇由两部分组成的综合博客 post,就像他们写的其他所有内容一样,如果您是 VSCode 和 PowerShell 的新手,这本书非常值得一读。
编辑:我知道这个问题已经有好几年了,但在我找到任何关于如何在 VSCode
中设置 PowerShell 调试的最新信息之前,我遇到了它"Integrated Terminal" 现在已集成到 Visual Studio 代码中。这是 Windows 中的 PowerShell 终端。 (在 Linux / MacOS 中可能 BASH)。终端显示在代码编辑器底部的面板中,而不是命令面板中。
打开终端的键盘快捷键:CTRL + `
来源:
- https://code.visualstudio.com/shortcuts/keyboard-shortcuts-windows.pdf
- https://code.visualstudio.com/docs/getstarted/keybindings
我在1.19.2版本确认过。不确定何时首次集成该功能。