powershell - 在 VSCode 集成终端内运行时如何自定义 powershell?

标签 powershell visual-studio-code

我想包括一些仅在 VSCode 内部运行时才会发生的 Powershell 自定义。

在 ISE 或包管理控制台内(在 Visual Studio 内)时,我可以使用 $host.name 的值切换行为或在“主机特定”powershell 配置文件中包含特定代码(例如 Microsoft.PowerShell_profile.ps1 )。

VSCode 中的 Powershell 看起来与在常规控制台中运行的 Powershell 相同,因此这些选项中的大多数都不可用。有没有办法解决这个问题?

最佳答案

另见 Visual Studio Code $psise equivalent

您可以检查 $psISE,如果它不存在,请尝试 $context = [Microsoft.Powershell.EditorServices.Extensions.EditorContext]$psEditor.GetEditorContext()但请注意,这会导致 ISE。

我在很多脚本中都使用了这个片段,所以我总是可以引用“$root”。

# Makes debugging from ISE easier.
if ($PSScriptRoot -eq "")
{
    if ($psISE)
    {
        $root = Split-Path -Parent $psISE.CurrentFile.FullPath
    }
    else
    {
        $context = $psEditor.GetEditorContext()
        $root = Split-Path -Parent $context.CurrentFile.Path
    }
}
else
{
    $root = $PSScriptRoot
}

关于powershell - 在 VSCode 集成终端内运行时如何自定义 powershell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48511874/

相关文章:

excel - PowerShell - 将数据粘贴到 Excel 中

powershell - 为什么括号会改变迭代类型?

powershell - 如何确保为 PowerShell 脚本模块中的每个异常调用日志记录 Cmdlet?

visual-studio-code - VS Code - 有没有办法计算项目中的所有代码行?

visual-studio-code - 如何在 VS Code 中轻松编辑 Jupyter 笔记本的 JSON 代码?

visual-studio-code - 是否可以打开和编辑 vsix 包(VS 代码的扩展文件)?如果是,那么如何?

powershell - Powershell添加不需要的小数(加法)

powershell - 如何在 Powershell 脚本中添加凭据参数?

visual-studio-code - 在 VSCode 中从远程 ssh 主机打开文件

html - VSCode 中的自动缩进 <head> 和 <body>