How to configure a VSCode Task to run a powershell script in the integrated terminal
Asked Answered
S

3

11

In such a way that it is not in a sub shell. I need it to be able to prepare the environment...set environment variable.

"version": "0.1.0",
"command": "${workspaceFolder}/Invoke-Task.ps1",
/*"command": "powershell",   creates subshell so doesn't work*/
"isShellCommand": false,
"args": [],
"showOutput": "always",
"echoCommand": true,
"suppressTaskName": true,
"tasks": [
    {
        "taskName": "task1",
        "args": ["task1"]
    },
    {
        "taskName": "task2",
        "args": ["task2"]
    }
]
Sisterinlaw answered 1/9, 2018 at 3:35 Comment(2)
Your question is not clear. What exactly you want to do? – Chufa
Take a look to my answer I think it will solve ur issue – Frady
F
11

I am sorry, but you are not correctly editing the .vscode/tasks.json file.

In your scenario, lets say you have some powershell script ./scripts/mycustomPSscript.ps1 you want to run as a vscode task. For such goal, edit the tasks file so it follows the below example:

{
    "version": "2.0.0",
    "tasks": [  
        {
            "label": "Run-some-custom-script",
            "detail": "Prepare some ENV variables for the deployment",
            "type": "shell",
            "command": "./../scripts/mycustomPSscript.ps1",
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared",
                "showReuseMessage": true,
                "clear": false
            }
        }
    ]
}
Frady answered 20/4, 2020 at 16:45 Comment(3)
Hey, can you explain what all this does? This looks completely different from what it's described in learn.microsoft.com/en-us/visualstudio/ide/… . Whey do you get doc for all this? They use "0.2.1" you use "2.0.0"... I would like to have task for all .ps1 files. I've modified their example to this: {"taskLabel": "Script","appliesTo":"*.ps1","type": "launch","command": "${env.COMSPEC}","args": ["${file}"]}. It appear in context menu but it just show file in notepad πŸ™„. In type I can only specify defualt, launch or.. – Rockefeller
...or msbuild. – Rockefeller
ah, sorry I didn't noticed it's for VS CODE... – Rockefeller
K
1

This has been doable since 2017, if I get your ask correctly.

integrated-terminal-tasks README This extension allows a workspace to define specific tasks that should be ran in VSCode's interactive terminal

https://marketplace.visualstudio.com/items?itemName=ntc.integrated-terminal-tasks

Also, your post / query, could been seen as a duplicate of this...

Run Code on integrated terminal Visual Studio Code

Korten answered 1/9, 2018 at 21:26 Comment(0)
A
0

Adding this configuration in the launch.json file did the trick for me

 "version": "0.2.0",
"configurations": [
    {
        "type": "PowerShell",
        "request": "launch",
        "name": "PowerShell Launch Current File",
        "script": "put full path here\\launch.ps1",
        "args": ["${file}"],
        "cwd": "${file}"
    },...

Not sure what you mean by 'integrated terminal' but the output does show up in the VSC terminal if this is what you're referring to.

Akkad answered 21/12, 2018 at 1:54 Comment(1)
that's not a task, question is about creating a task. – Lipoprotein

© 2022 - 2024 β€” McMap. All rights reserved.