简体   繁体   中英

Visual Studio Code: Powershell Greenhorn

My Code:

$a = $env:Path
$b = $a.Split(";")
Write-host $b
$a
$b

The automatic Help in Powershell advises me to Surround with Enum, funtion ....

What am I advised to do? An explanation, some sample code could help

在此处输入图像描述

Thx in advance

VSCode suggests that you should surround $b[3] with a function because this is stuff that on first glance isn't that maintainable. I see you want the 3rd path of the path variable but don't know what exactly you are looking for, neither does anyone that will potentially maintain the script so you should wrap this line into something meaningfull.

Take this as an example:

    function Get-VSCodePath 
    {
        $paths = ($env:Path).Split(';')
        $vsCodePath = $paths | Where-Object { ($_.Contains("VS Code") -eq $true) }
        Write-Output $vsCodePath
    }

$vscodePath = Get-VSCodePath
Write-Host $vscodePath

i wrapped my search for a specific path into a function with meaningfull name so whoever maintains the script will know what i searched for

It's not obvious from your screenshot, but the crucial aspect is that you've selected $b[3] .

Whenever you select a multi-character range , Visual Studio's PowerShell extension offers you potentially useful actions , indicated by and accessible via the lightbulb icon :

  • You can click on the icon or use Control-. (period) to invoke the menu of available actions.

  • Actions of a general nature that are always presented pertain to surrounding (enclosing) the selected text with various constructs, such as shown in your screenshot.

    • This is a convenient way to make the selected text part of a larger language construct, such as an if statement or a function definition.

    • These actions are not prescriptive , ie it is up to you to decide whether any of the actions presented make sense in the context of what you're trying to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM