首页 > 解决方案 > 如何在 Visual Studio Code 中将 Powershell 脚本作为 noprofile 启动

问题描述

如何在 Visual Studio Code 中将 Powershell 脚本作为 noprofile 启动,我可以使用命令PowerShell_Ise -NoProfile运行带有 noprofile 的 Powershell Ise 。但是我们如何在 Visual Studio Code 中为 poershell 会话做同样的事情。

标签: powershellshellvisual-studio-code

解决方案


  • 如果您在PowerShell 集成控制台中运行 PowerShell,这是一个带有PowerShell 扩展的特殊外壳:

    • 要关闭此特殊 shell 中的配置文件加载,请确保未选中
      PowerShell: Enable Profile LoadingPowerShell 扩展选项(通过、)。File > Preferences > SettingsCtrl-,

    • 有关如何控制在 PowerShell 集成控制台中使用的特定 PowerShell 版本/可执行文件,请参阅底部部分。

  • 如果您在 Visual Studio Code 的集成终端中将PowerShell 作为通用 shell运行:

    • 您必须修改默认的 PowerShell shell 配置文件或添加自定义配置文件,"args"参数值为[ "-noprofile" ],通过直接编辑设置下的 JSON 文件settings.json>Preferences: Open Settings (JSON)从命令面板)。

    • 下面显示了一个相关settings.json摘录,其中包含已修改的默认 PowerShell 配置文件,该配置文件禁止加载配置文件。

"terminal.integrated.profiles.windows": {
    "PowerShell": {
      "source": "PowerShell",
      "icon": "terminal-powershell",
      "args": [ "-noprofile" ]  // suppress profile loading
    },  // ...
}

继续阅读以了解有关 Visual Studio Code 中的 shell 和终端的详细信息。


Visual Studio Code 中的 shell 和终端设置概述:

  • 3 种不同类型的外壳本质上适用

    • 集成终端的默认外壳(TERMINAL面板的选项卡)。

      • 可选地,用于自动化任务的自动化 shelltasks.json (在 中定义)而不是默认的集成终端 shell。
    • 打开外部终端的默认外壳(> Open New External Terminal);请注意,在 Windows 上,您可以直接指定 shell 可执行文件,但在类 Unix 平台上,您必须指定终端应用程序,然后由终端应用程序自己确定要启动的 shell - 请参阅下面的详细信息。

  • 安装PowerShell 扩展后,另一个 shell 应用:

    • 用于PowerShell 集成控制台的特定 PowerShell 可执行文件,它提供与编辑的紧密集成并支持调试 PowerShell 代码。

这些贝壳:

  • 都可以单独配置

  • 它们的默认行为可能不同

  • 只有其中一些允许您指定启动参数,例如-NoProfile您的情况。

  • 默认外壳是:

    • 对于集成终端和运行任务:
      • 视窗:PowerShell
        • 请注意,如果发现安装了PowerShell (Core) 6+版本,则它优先于内置的Windows PowerShell版本。
      • 类 Unix 平台:用户的默认 shell,反映在SHELL环境变量中。
    • 对于外部终端:
      • Windows: conhost.exe,启动cmd.exe(命令提示符)
      • 类 Unix 平台:主机平台的默认终端应用程序,例如Terminal.app在 macOS 上,它自己决定要启动的 shell(尽管默认情况下,它也是用户的默认 shell)。
      • 注意:仅在 Windows 上,您可以直接指定shell(而不是终端)可执行文件(例如,bash.exe),在这种情况下,它会在常规控制台窗口中打开(conhost.exe

以下文件摘录Settings.json( > Preferences: Open Settings (JSON)) 显示了每个文件的相关设置(从 VSCode v1.60 / PowerShell Extension v2021.8.2 开始):

  • 早期的VSCode 版本中,"terminal.integrated.shell.*""terminal.integrated.shellArgs.*"设置决定了集成终端的默认 shell 及其启动参数。这些已被通过属性定义的shell配置文件以及包含默认使用的配置文件名称"terminal.integrated.profiles.*"的关联属性所取代,如下所示。"terminal.integrated.defaultProfile.*"
{ 

  // ...

  // **General-purpose integrated-terminal shell**.

  // Shell *profiles* define the *available* shells for the integrated terminal.
  // This property is situationally created automatically, platform-appropriately,
  // based on what shells VSCode finds in standard locations on your 
  // system.
  // However, it *need not be present* in a given file - VSCode
  // knows about about *standard* profiles *implicitly* when it
  // comes to choosing a default shell.
  // This example applies to Windows, and shows that Git Bash
  // was found on the system.
  // On Unix-like platforms, replace ".windows" with ".osx" or ".linux", 
  // as appropriate.
  // To add custom profiles:
  //   * In file *paths*, use "\\" or "/" as the path separator.
  //   * Use an "args" array property to specify start-up arguments, if necessary.
  "terminal.integrated.profiles.windows": {
      "PowerShell": {
        "source": "PowerShell",
        "icon": "terminal-powershell"
      },
      "Command Prompt": {
        "path": [
          "${env:windir}\\Sysnative\\cmd.exe",
          "${env:windir}\\System32\\cmd.exe"
        ],
        "args": [],
        "icon": "terminal-cmd"
      },
      "Git Bash": {
        "source": "Git Bash"
      }
  }

  // Define the *default* shell profile, which serves as the default
  // shell in the *integrated terminal* and - except
  // if overridden, as shown below - also for *tasks*.
  "terminal.integrated.defaultProfile.windows": "PowerShell"  
  
  // **Automation-tasks shell**,
  // for the tasks defined in "tasks.json" and for debugging:
  // This definition is *optional* and *overrides* the default shell configured above.
  // Note: 
  //  * The *executable file path* must be specified (just the file name is sufficient for executables present in %PATH%);
  //    that is, this setting doesn't reference the shell *profiles*.
  //  * There is NO way to pass startup arguments.
  "terminal.integrated.automationShell.windows": "cmd.exe",

  // **External-terminal executable**:
  // The *terminal program* to use for opening an external terminal window, which itself determines what shell to launch.
  // (> Open New External Terminal).
  // Note: 
  //  * The *executable file path* must be specified (just the file name is sufficient for executables present in %PATH%);
  //  * There is NO way to pass startup arguments.
  //  * This example specifies Windows Terminal (wt.exe).
  //   * On Windows only, you may also specify a *shell* executable directly,
  //     which then opens in a regular console window (conhost.exe)
  "terminal.external.windowsExec": "wt.exe",

  // **PowerShell Integrated Console**:
  // Profile loading is *disabled* by default; you can enable it here, but 
  // note that the PowerShell Integrated Console has its own, 
  // separate $PROFILE location, which differs from the one in a 
  // regular console window. If you want to load your regular profile, 
  // place the following statement in the $PROFILE file of 
  // the Integrated Console:
  //    . ($PROFILE -replace '\.VSCode', '.PowerShell')
  // (Open the profile file for editing by submitting the following command 
  // from the Integrated Console: 
  //    code $PROFILE
  // )
  "powershell.enableProfileLoading": false,
  
  // ...

}

如果要将 PowerShell 集成控制台配置为使用不同的 PowerShell 版本/版本

  • GUI 方法:在 VSCode 面板(屏幕下半部分)的选项卡中激活 PowerShell 集成控制台Terminal,单击右下角的版本号图标(例如,PowerShell 版本选择器

    • 选择不同的版本(如果存在),前缀为Switch to:
    • 如果未显示感兴趣的版本/版本,则必须通过Settings.json文件添加其可执行路径(请参阅下一点)。
  • 通过settings.json( > Preferences: Open Settings (JSON)):

    • 数组值powershell.powerShellAdditionalExePaths属性允许您添加扩展无法自动找到的 PowerShell 版本的完整可执行路径 - 请参见下面的示例。

    • powershell.powerShellDefaultVersion属性决定了默认使用哪个版本;由于您必须通过其显示名称指定它,其中包括为自动发现的版本自动选择的显示名称,因此通过 GUI 进行选择是最简单的,如上所示。

{ 

  // ...

  // The paths to any PowerShell executables that the extension cannot auto-discover.
  // The "versionName" is a self-chosen name that is offered in the 
  // version-selector menu that pops up when you click on the version number 
  // near the right edge of the status bar when the 
  // PowerShell Integrated Console is active.
  // (The currently active version is displayed by its actual characteristics,
  //  not by its "versionName" property; e.g., 
  //  "PowerShell 7.0 (X64) Core Edition [7.0.0]")
  "powershell.powerShellAdditionalExePaths": [ 
    { 
      "versionName": "Latest Preview", 
      "exePath": "C:\\Users\\jdoe\\AppData\\Local\\Microsoft\\powershell\\pwsh.exe" 
    } 
  ],

  // The "versionName" property of the PowerShell executable to use by default.
  // Note: To switch to an executable that the extension found automatically,
  //       it is simplest to use the version-selector menu.
  "powershell.powerShellDefaultVersion": "Latest Preview",

  // ...

}

推荐阅读