简体   繁体   English

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

[英]How can you reset the PowerShell runspace within Visual Studio Code

I'm currently using version 0.10.0 of the PowerShell extension within Visual Studio Code. 我目前在Visual Studio Code中使用版本0.10.0的PowerShell扩展。 It used to be that the run space was reset between each run of a script so if there were changes made to a module, the updated module was imported into the script. 过去,每次运行脚本都会重置运行空间,因此如果对模块进行了更改,则会将更新后的模块导入到脚本中。

However, for some reason, the PowerShell run space is no longer being reset so a change in a module is not picked up during the next run. 但是,由于某种原因,PowerShell运行空间不再被重置,因此在下次运行期间不会拾取模块中的更改。

Is there a setting within the PowerShell extension that will ensure the run space is reset between each run of a script? PowerShell扩展中是否有一个设置可确保在每次运行脚本之间重置运行空间?

The setting powershell.debugging.createTemporaryIntegratedConsole which is now available with in the PowerShell plugin for Visual Studio Code solved my problem. 设置powershell.debugging.createTemporaryIntegratedConsole现在可以在Visual Studio Code的PowerShell插件中使用,解决了我的问题。

The setting is defaulted to false so it must be enabled before it will work. 该设置默认为false,因此必须先启用它才能生效。

When it is set to true the PowerShell session is reset each time you try to debug through a script. 当它设置为true时,每次尝试通过脚本进行调试时都会重置PowerShell会话。 This means that: 这意味着:

  • Constant variables can be reset 可以重置常量变量
  • Updates to PowerShell modules will be pulled into the session PowerShell模块的更新将被拉入会话

It can be enabled by adding the following line to the user settings JSON file 可以通过将以下行添加到用户设置JSON文件来启用它

"powershell.debugging.createTemporaryIntegratedConsole": true

Or enable it in the Preferences under File -> Preferences -> Settings and search for Create Temporary Integrated Console 或者在文件 - >首选项 - >设置下的首选项中启用它,然后搜索创建临时集成控制台

I still dont have a good answer for this, but this is what I did to mitigate the problem during development. 我仍然没有一个很好的答案,但这是我在开发过程中为减轻问题所做的。

I noticed that VS Code was caching the variables between each run, and that it effectively ran the script as if it was a REPL. 我注意到VS Code在每次运行之间缓存变量,并且它有效地运行了脚本,就像它是一个REPL一样。

Due to the nature of PowerShell scopes, you can wrap your code in an Invoke-Command -ScriptBlock {} and as variables are not accessible outside of their script block, you can re-run the code without worrying about leaving objects in vs code. 由于PowerShell作用域的性质,您可以将代码包装在Invoke-Command -ScriptBlock {}并且由于变量不能在其脚本块之外访问,因此您可以重新运行代码而无需担心在vs代码中保留对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM