Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
698 views
in Technique[技术] by (71.8m points)

visual studio code - How can I customize powershell when running inside of the VSCode integrated terminal?

I would like to include a few Powershell customizations that only occur when running inside of VSCode.

When inside of ISE or Package Management Console (inside Visual Studio), I can use the value of $host.name to switch behavior or include specific code in the "host specific" powershell profile (e.g. Microsoft.PowerShell_profile.ps1).

Powershell inside of VSCode appears identical to Powershell running in a regular console so most of these options aren't available. Is there anyway to workaround this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

See also Visual Studio Code $psise equivalent

You may check for $psISE, and if it does not exist, try $context = [Microsoft.Powershell.EditorServices.Extensions.EditorContext]$psEditor.GetEditorContext() but be aware, this will throw in ISE.

I use this snippet in a lot of my scripts, so I can always reference "$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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...