簡體   English   中英

如何在 Visual Studio Code 中以 noprofile 的形式啟動 Powershell 腳本

[英]How can I start Powershell script as noprofile in Visual Studio Code

如何在 Visual Studio Code 中將 Powershell 腳本作為 noprofile 啟動,我可以使用命令PowerShell_Ise -NoProfile運行帶有 noprofile 的 Powershell Ise。 但是我們如何在 Visual Studio Code 中為 poershell 會話做同樣的事情。

  • 如果您PowerShell 集成控制台中運行 PowerShell ,它是PowerShell 擴展附帶的特殊shell:

    • 要關閉此特殊 shell 中的配置文件加載,請確保
      PowerShell: Enable Profile Loading未選中PowerShell 擴展的PowerShell: Enable Profile Loading選項(通過File > Preferences > Settings , Ctrl-, )。

    • 有關如何控制在 PowerShell 集成控制台中使用的特定 PowerShell 版本/可執行文件,請參閱底部部分。

  • 如果您在 Visual Studio Code 的集成終端中將PowerShell 作為通用 shell運行

    • 您必須修改默認的 PowerShell shell 配置文件或添加自定義配置文件, "args"參數值為[ "-noprofile" ] ,通過直接編輯設置基礎的 JSON 文件settings.json ( >Preferences: Open 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選項卡)。

      • 可選地,用於自動化任務的自動化 shell (在tasks.json定義)而不是默認的集成終端 shell。
    • 用於打開外部終端的默認 shell ( > Open New External Terminal ); 請注意,在 Windows 上,您可以直接指定 shell 可執行文件,但在類 Unix 平台上,您必須指定一個終端應用程序,然后它本身決定要啟動的 shell - 請參閱下面的詳細信息。

  • 安裝了PowerShell 擴展后,另一個外殼適用:

    • 用於PowerShell 集成控制台的特定 PowerShell 可執行文件,它提供與編輯的緊密集成並支持調試 PowerShell 代碼。

這些貝殼:

  • 都可以單獨配置

  • 它們的默認行為可能有所不同

  • 只有其中一些允許您指定啟動參數,例如-NoProfile在您的情況下。

  • 默認外殼是:

    • 對於集成終端和運行任務:
      • 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配置文件取代,通過"terminal.integrated.profiles.*"屬性和關聯的"terminal.integrated.defaultProfile.*"屬性定義,該屬性包含默認使用的配置文件的名稱,如下所示。 從 v1.60 開始,這些已棄用的設置現已過時已停止工作
{ 

  // ...

  // **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 集成控制台,單擊右下角的版本號圖標(例如, 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",

  // ...

}

您可以進入 powershell 擴展設置並刪除“PowerShell:啟用配置文件加載”中的復選框,我認為它有幫助。 還要檢查任務以運行 powershell 腳本,並帶有一些關於任務的參數討論

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM