-->

How to configure a task to start a .ps1-script in

2019-05-22 20:53发布

问题:

I want to use the Visual Studio Code IDE ("VSC") to develop in MQL (rather than in the native MetaEditor IDE) as described here: How to code & compile MQL5 in Visual Studio.

My question refers to the compiling process, which consists of a VSC-task that calls a PowerShell script which in turn invokes MetaEditor.exe and dynamically passes to it the current .mq5-file to be compiled (that's why a task is used).

Everything works fine when I run the PowerShell script directly (by selecting its code and hitting F8), but when I try to run it via the designated VSC-task I get the error...

The terminal process terminated with exit code: 1

...even though I chose PowerShell as the default shell to be used by VSC (instead of cmd, this is my according setting: "terminal.integrated.shell.windows": "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe").

This is the VSC-task in .json-format, version 2.0.0, that I'm talking about:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Compile-MQL",
            "type": "shell",
            "command": "C:\\Users\\Username\\AppData\\Roaming\\MetaQuotes\\Terminal\\D0E8209F77C8CF37AD8BF550E51FF075\\MQL5\\Compile-MQL.ps1 ${file}",
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared",
                "showReuseMessage": false
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

Can somebody please adapt my above VSC-task so that it works out of the box?

@postanote: please DO NOT copy-paste the answer to similar questions here again since I am unfortunately not able to translate version 0.1.0 to 2.0.0 (or any other deviations), I'm sure there's someone out there who can quickly adapt my few lines of code so that they work right away...

Many thanks in advance!

PS: this is the above-mentioned PowerShell script (which works with F8):

#gets the File To Compile as an external parameter... Defaults to a Test file...
Param($FileToCompile = "C:\Users\Username\AppData\Roaming\MetaQuotes\Terminal\D0E8209F77C8CF37AD8BF550E51FF075\MQL5\Experts\Advisors\ExpertMACD.mq5")

#cleans the terminal screen and sets the log file name...
Clear-Host
$LogFile = $FileToCompile + ".log"
& "C:\Users\Username\AppData\Roaming\MetaQuotes\Terminal\D0E8209F77C8CF37AD8BF550E51FF075\MQL5\compile.bat" "C:\Program Files\MetaTrader 5\metaeditor64.exe" "$FileToCompile" "$LogFile" "C:\Users\Username\AppData\Roaming\MetaQuotes\Terminal\D0E8209F77C8CF37AD8BF550E51FF075\MQL5"

#before continue check if the Compile File has any spaces in it...
if ($FileToCompile.Contains(" ")) {
    "";"";
    Write-Host "ERROR!  Impossible to Compile! Your Filename or Path contains SPACES!" -ForegroundColor Red;
    "";
    Write-Host $FileToCompile -ForegroundColor Red;
    "";"";
    return;
}

#first of all, kill MT Terminal (if running)... otherwise it will not see the new compiled version of the code...
Get-Process -Name terminal64 -ErrorAction SilentlyContinue |
    Where-Object {$_.Id -gt 0} |
    Stop-Process

#fires up the Metaeditor compiler...
& "C:\Program Files\MetaTrader 5\metaeditor64.exe" /compile:"$FileToCompile" /log:"$LogFile" /inc:"C:\Users\Username\AppData\Roaming\MetaQuotes\Terminal\D0E8209F77C8CF37AD8BF550E51FF075\MQL5" | Out-Null

#get some clean real state and tells the user what is being compiled (just the file name, no path)...
"";"";"";"";""
$JustTheFileName = Split-Path $FileToCompile -Leaf
Write-Host "Compiling........: $JustTheFileName"
""

#reads the log file. Eliminates the blank lines. Skip the first line because it is useless.
$Log = Get-Content -Path $LogFile |
       Where-Object {$_ -ne ""} |
       Select-Object -Skip 1

#Green color for successful Compilation. Otherwise (error/warning), Red!
$WhichColor = "Red"
$Log | ForEach-Object {
    if ($_.Contains("0 error(s), 0 warning(s)")) {
        $WhichColor="Green"
    }
}

#runs through all the log lines...
$Log | ForEach-Object {
     #ignores the ": information: error generating code" line when ME was successful
     if (-not $_.Contains("information:")) {
          #common log line... just print it...
          Write-Host $_ -ForegroundColor $WhichColor
     }
}

#get the MT Terminal back if all went well...
if ($WhichColor -eq "Green") {
    & "c:\program files\metatrader 5\terminal64.exe"
}

PS2: the MetaEditor IDE can be installed for free together with MetaTrader 5 here.

回答1:

In the meantime I've found the reason myself: my default Windows Powershell Security Settings prevented the execution of my .ps1-script. The solution was to run PowerShell as administrator (Win+x → Windows PowerShell (administrator)) and run the command

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser

→ confirm.

Here is some background information for the interested reader:

About Execution Policies

Set Execution Policy