skip to Main Content

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

enter image description here

Thx in advance

2

Answers


  1. 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

    Login or Signup to reply.
  2. 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, i.e. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search