![](/img/trans.png)
[英]How can I run my Powershell Script inside of Visual Studio Code?
[英]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 和終端的詳細一般信息。
3 種不同類型的外殼本質上適用:
集成終端的默認 shell(面板的TERMINAL
選項卡)。
tasks.json
定義)而不是默認的集成終端 shell。 用於打開外部終端的默認 shell ( > Open New External Terminal
); 請注意,在 Windows 上,您可以直接指定 shell 可執行文件,但在類 Unix 平台上,您必須指定一個終端應用程序,然后它本身決定要啟動的 shell - 請參閱下面的詳細信息。
安裝了PowerShell 擴展后,另一個外殼適用:
這些貝殼:
都可以單獨配置
它們的默認行為可能有所不同
只有其中一些允許您指定啟動參數,例如-NoProfile
在您的情況下。
默認外殼是:
SHELL
環境變量中。conhost.exe
,它啟動cmd.exe
(命令提示符)Terminal.app
,它本身決定要啟動的 shell(盡管默認情況下,它也是用戶的默認 shell)。bash.exe
),在這種情況下,它會在常規控制台窗口( conhost.exe
)中打開以下摘自Settings.json
文件( > Preferences: Open Settings (JSON)
)顯示了每個文件的相關設置(從 VSCode v1.60 / PowerShell Extension v2021.8.2 開始):
"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 集成控制台,單擊右下角的版本號圖標(例如, )
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",
// ...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.