【问题标题】:How can I start Powershell script as noprofile in Visual Studio Code如何在 Visual Studio Code 中将 Powershell 脚本作为 noprofile 启动
【发布时间】:2020-06-17 14:40:47
【问题描述】:

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

【问题讨论】:

    标签: powershell shell visual-studio-code


    【解决方案1】:
    • 如果您PowerShell 集成控制台 中运行 PowerShell,这是 PowerShell extension 附带的一个 特殊 shell:

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

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

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

      • 您必须修改默认的 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 种不同类型的 shell 本质上适用

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

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

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

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

    这些贝壳:

    • 都可以单独配置

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

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

    • 默认 shell是:

      • 对于集成终端和正在运行的任务:
        • Windows:PowerShell
          • 请注意,如果发现安装了 PowerShell (Core) 6+ 版本,则它优先于内置的 Windows PowerShell 版本。
        • 类 Unix 平台:用户的默认 shell,反映在 SHELL 环境变量中。
      • 对于外部终端:
        • Windows:conhost.exe,启动 cmd.exe(命令提示符)
        • 类 Unix 平台:宿主平台的默认终端应用程序,例如 macOS 上的Terminal.app,它本身决定要启动的 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 profiles(通过"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 面板中的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",
    
      // ...
    
    }
    

    【讨论】:

      【解决方案2】:

      您可以进入 powershell 扩展设置并删除“PowerShell:启用配置文件加载”处的复选框,我认为它会有所帮助。还要检查任务是否运行带有一些参数的powershell脚本discussion about task

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-30
        • 1970-01-01
        • 2021-02-01
        • 2018-04-11
        • 2020-01-02
        • 2022-11-02
        • 1970-01-01
        • 2016-01-01
        相关资源
        最近更新 更多