0

Due to company constraints I cannot immediately execute PowerShell commands:

File C:...Activate.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see 
about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170

The workaround I already found (thanks Stack!) is to execute, in the command line (not as a PS script):

Set-ExecutionPolicy Unrestricted -Scope Process

This solves the problem and when I'm running code for real, it works well. When I am in the middle of a development/debugging session in VS Code, however, this command does not execute.

Is there a place in one of the JSON setup files where I can pre-execute the above command before PS or cmd or the python debugger attempts to run Activate.ps1? This fixes venv problems for me and anyone else with problematic corporate script execution policies.

JialeDu
  • 6,021
  • 2
  • 5
  • 24

1 Answers1

0

VS Code supports adding tasks.json files to set up automatic tasks.

A simple example:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "test",
            "type": "shell",
            "command": "dir",
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared",
                "showReuseMessage": true,
                "clear": false
            },
            "runOptions": {
                "runOn": "folderOpen"
            }
        }
    ]
}

This setting is to run the dir command to read the files in the directory when opening the folder.

enter image description here

How to create a task.json file?

  1. Open command palette with Ctrl+Shift+P

  2. Search and select Tasks:Configure Task

    configure task

  3. Choose Create task.json file fromtemplate

    creshhssshsshfhate

  4. Choose Others

    enter image description here

More about tasks.json: Click here

PS:

Regarding debugging, the launch.json file can also help a lot. Click here and here to know more.

JialeDu
  • 6,021
  • 2
  • 5
  • 24
  • Thank you, that's very likely the right track for this. In and of itself this cannot solve the problem, because the command executed from tasks.json executes in a separate process from the terminal (PowerShell) process, and the present scope of the executed command is per-process. By changing "-Scope Process" to "-Scope CurrentUser" I massively increase the security threat area but also solve the problem. If there is a way to execute a task *inside of a new terminal instance, when the instance is started*, that would actually solve the problem safely. – Hugh Eisenman Jul 02 '22 at 02:50