如何在Visual Studio代码中将Powershell脚本作为noprofile启动

zysjyyx4  于 2022-12-19  发布在  Shell
关注(0)|答案(2)|浏览(251)

如何在Visual Studio代码中以noprofile启动Powershell脚本,我可以使用命令PowerShell_Ise -NoProfile运行带有noprofile的Powershell伊势。但如何在Visual Studio代码中对poershell会话执行相同的操作?

w1jd8yoj

w1jd8yoj1#

  • 如果您**在PIC(*PowerShell集成控制台 *)**中运行PowerShell,这是PowerShell extension附带的一个 * 特殊 * shell:
  • 若要关闭这个特殊shell中的概要文件加载,请确保

PowerShell扩展的PowerShell: Enable Profile Loading选项 * 未选中 *(通过File > Preferences > Settings、Ctrl-、)。

  • 有关如何控制在PIC(PowerShell集成控制台)中使用哪个特定PowerShell版本/可执行文件,请参见底部。
  • 如果您在Visual Studio Code的集成终端中将PowerShell作为 * 通用shell*运行:
  • 您必须修改默认的PowerShell *shell配置文件 * 或添加一个自定义配置文件,其中"args"参数值为[ "-noprofile" ],方法是直接编辑设置settings.json(命令面板中的>Preferences: Open Settings (JSON))的JSON文件。
  • 下面显示了一个相关的settings.json摘录,其中包含一个修改后的默认PowerShell配置文件,该配置文件抑制了配置文件加载。
"terminal.integrated.profiles.windows": {
    "PowerShell": {
      "source": "PowerShell",
      "icon": "terminal-powershell",
      "args": [ "-noprofile" ]  // suppress profile loading
    },  // ...
}

请继续阅读,了解有关Visual Studio代码中 shell 和终端的详细常规信息。

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

3种不同类型的 shell 适用
**集成端子
的默认 shell (面板的TERMINAL选项卡)。

    • 可选 ,用于自动化任务的自动化shell*(在tasks.json中定义),而不是默认的集成终端shell。
  • 打开***外部终端***的默认shell(> Open New External Terminal);注意,在Windows上你可以 * 直接 * 指定一个shell可执行文件,但是在类Unix平台上你必须指定一个 * 终端应用程序 *,它 * 自己 * 决定要启动的shell-参见下面的详细信息。
  • 安装了**PowerShell extension**后,还会应用另一个shell:
  • 用于**PIC(*PowerShell集成控制台 *)**的特定PowerShell可执行文件,它提供与编辑的紧密集成,并支持调试PowerShell代码。

这些壳体:

  • 都可以单独配置
  • 它们的默认行为可能不同
  • 它们中只有 * 一些 * 允许你指定 * 启动参数 *,比如在你的例子中的-NoProfile
    ****默认 * shell**为:
  • 对于 * 集成 * 终端和运行任务:
  • Windows:PowerShell
  • 请注意,如果发现安装了 PowerShell(核心)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扩展v2021.8.2起):

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

  // ...

  // **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,
  
  // ...

}

*如果要配置PIC(PowerShell集成控制台 )以使用 * 不同的PowerShell版本

注意:从PowerShell扩展的v2022.6.3版本开始编写。

GUI方法:

  • 确保PowerShell源代码文件是活动编辑器。
  • 单击状态栏(右下角)中的{},然后在弹出的工具提示窗口中单击Show PowerShell Session Menu,如下图所示。
  • 或者,无论活动编辑器是否包含PowerShell源代码,都可以在命令面板中提交PowerShell: Show Session Menu以打开相关菜单。


    • 如果 * 会话菜单被 * 固定 * 为图标(使用工具提示窗口右边缘的图钉图标进行切换),它看起来如下所示,反映活动版本 * 编号 直接 * 单击它将在命令面板中打开PowerShell会话菜单:


  • 这将在命令调色板中打开PowerShell会话菜单:从其他可用版本(如果有的话)中选择前缀为Switch to:的版本,这 * 使该版本成为以后的默认版本 *。

  • 如果未显示感兴趣的版本,您必须通过Settings GUI(从PowerShell会话菜单中选择Modify list of additional PowerShell locations)或通过settings.json文件(参见下一节)添加其可执行路径。
    通过x1米30纳米1 x(x1米31纳米1 x):

  • powershell.powerShellAdditionalExePaths设置是一个 * 单个对象 *,允许您添加扩展无法通过 * 自选属性名称 * 自动找到的PowerShell版本的完整可执行路径-请参见下面的示例。

  • powershell.powerShellDefaultVersion属性确定要使用的版本 * 默认情况下 * 是powershell.powerShellAdditionalExePaths设置的属性名称或自动发现的版本的名称,如会话菜单中所示。从菜单中选择Switch to:条目会自动更新此设置。

{ 

  // ...

  // The paths to any PowerShell executables that the extension cannot auto-discover.
  // The property names are self-chosen names, which show in the
  // PowerShell session menu.
  // Note: 
  //  * Executable paths only are supported, not also arguments.
  //  * If a value isn't a valid executable path, it is ignored,
  //    and the version doesn't show up in the session menu.
  // In the status bar, the currently active version is displayed by its actual characteristics,
  //  not by these self-chosen property names; e.g., 
  //  "PowerShell 7.2")
  "powershell.powerShellAdditionalExePaths": { 
      "Latest Preview": "C:\\Users\\jdoe\\AppData\\Local\\Microsoft\\powershell\\pwsh.exe" 
  },

  // The PowerShell version to use by default, which can be
  // a property name from "powershell.powerShellAdditionalExePaths" or
  // the name of an auto-discovered version, as show in the PowerShell
  // session menu.
  "powershell.powerShellDefaultVersion": "Latest Preview",

  // ...

}
j8ag8udp

j8ag8udp2#

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

相关问题