如何在 Visual Studio Code 中重置 PowerShell 运行空间

How can you reset the PowerShell runspace within Visual Studio Code

提问人:gerard 提问时间:3/17/2017 最后编辑:gerard 更新时间:8/15/2019 访问量:8928

问:

我目前在 Visual Studio Code 中使用 PowerShell 扩展的 0.10.0 版。过去,每次运行脚本之间都会重置运行空间,因此,如果对模块进行了更改,则更新后的模块会导入到脚本中。

但是,由于某种原因,PowerShell 运行空间不再重置,因此在下次运行期间不会选取模块中的更改。

PowerShell 扩展中是否有一项设置可以确保在每次运行脚本之间重置运行空间?

PowerShell 可视化 Studio 代码 PowerShell-3.0

评论


答:

4赞 ConstantineK 9/29/2017 #1

我仍然没有一个很好的答案,但这就是我在开发过程中为缓解问题所做的。

我注意到 VS Code 在每次运行之间缓存变量,并且它有效地运行脚本,就好像它是 REPL 一样。

由于 PowerShell 范围的性质,你可以将代码包装在 并且由于变量无法在其脚本块之外访问,因此可以重新运行代码,而不必担心将对象保留在 vs 代码中。Invoke-Command -ScriptBlock {}

23赞 gerard 10/2/2017 #2

现在在Visual Studio Code的PowerShell插件中可用的设置解决了我的问题。powershell.debugging.createTemporaryIntegratedConsole

该设置默认为 false,因此必须先启用它才能工作。

当它设置为 true 时,每次尝试通过脚本进行调试时,都会重置 PowerShell 会话。这意味着:

  • 常量变量可以重置
  • 对 PowerShell 模块的更新将拉取到会话中

可以通过将以下行添加到用户设置 JSON 文件来启用它

"powershell.debugging.createTemporaryIntegratedConsole": true

或者在“文件”-“>首选项”->“设置”下的“首选项”中启用它,然后搜索“创建临时集成控制台”

评论

2赞 Moss 10/7/2019
不幸的是,“TemporaryIntegratedConsole”有一个 1 1/2 年未修复的错误,它阻止了交互式调试脚本。github.com/PowerShell/vscode-powershell/issues/1263