The terminal process terminated with exit code: 1 - json

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 invokes MetaEditor.exe to perform the actual compiling.
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
(before I chose PowerShell as the default shell as described in the linked description).
This is the 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"
}
and this is the VSC-task in .json-format that should call the previous PowerShell script (but ends in the abovementioned error):
{
// 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 tell me how to get rid of this error?
PS: to reproduce this issue, MetaTrader (which includes the MetaEditor IDE) needs to be downloaded (for free).

I had this issue because I accidentally deleted tsconfig.json

I had the same problem as you (I use windows 10).
Try the following:
If your computer is running powershell, turn it off.
Control Panel -> Programs -> Turn Windows features on or off
Search Window Powershell 2.0 -> Uncheckbox -> OK
Restart
I hope this could help you

The reason this works with F8 is because you are already in a PowerShell session and thus will run natively. Using your task, you are trying to run a .ps1, without starting PowerShell.
A task run, is a task run, the particulars (switches, args, etc.) of the other thing you are running in concert may have its own needs. However, your query could be potentially seen as a duplicate of this: ---
How do i configure a task to call a PowerShell script in vscode
# Accepted answer below:
{
"version": "0.1.0",
"command": "powershell",
"args": [
"-ExecutionPolicy",
"Unrestricted",
"-NoProfile",
"-File",
"${cwd}/source/deployment/build.ps1"
],
"taskSelector": "-task ",
"showOutput": "always",
"tasks": [
{
"taskName": "build",
"showOutput": "always",
"isBuildCommand": true
}
]
}
See also this MSDN Channel 9 video on Task Runners.
IMHO, if you have a .ps1 that already does what you want, then why call it from a task? Sure, you can but, you are already in VSC, just run your scripts from the VSCode PowerShell console terminal, simply by typing its name.
You also don't say how you have your VSCode user settings defined.
Example - what are the below set to in your use settings:
"terminal.integrated.shell.windows":
"powershell.powerShellExePath":
"shellLauncher.shells.windows":
Update for OP
It appears you post this query twice and I responded twice.
How to configure a task to start a .ps1-script in VSC IDE (version
2.0.0)?
Of course the solution is not mine, but from the Q&A I pointed you to.
See your other post where I pointed you to the VSCode docs about configuring your user environment for what terminals are to be used.
Your error specifically means that the shell process could not be launched, again, because it can't find what it needs because of what's in / not in your VSCode use settings.

The SOLUTION to my problem was simple: Stop the running project.
As for my case, it was my own fault, only that I did NOT know about it.
It was like this:
I was doing Node.js coding. When I tried to run "debug", I got the following error:
process exited with code 1
which means I could never debug.
I tried a few ways I found on the Internet, but none of them worked.
Then I found that my "nodemon" automatic start function was running.
So I stopped the running server, and I tried to run the debug function again. This time it started to work.

Related

How to set Variables in Release pipeline in Azure DevOps pipeline

I have searched all the documents about how to set variables to pass the variable during the run time build pipeline and only told me how to set in .yaml. But how to use it in release pipeline runtime variables?
Suppose I have a Generate-manifest.ps1 file which I will run to generate manifest.json file which representing the latest release packages with version number inside it. Version: 1.0.0. This version value should passed as a variable during runtime. I need help to do this.
Manifest.json file looks like this.:
{
"version": "1.0.0",
"timeStamp": "2021-05-07T09:41:34+00:00",
"packages": [
{
"name": "data-service",
"type": "docker-image",
"version": "REL-1.0.5367"
},
{
"name": "feedback-service",
"type": "docker-image",
"version": "REL-1.0.6099"
},
]
}
Based on your explanation I understand that you need to read version variable during the build pipeline and pass this variable on the release pipeline in order to use it on BuildNumber for example.
First you will need to use a powershell task to read the version value from the .json file.
$deployment_config = Get-Content manifest.json -raw | ConvertFrom-Json
$versionNumber = $deployment_config.version
Then you can change the BuildNumber variable on build pipeline
$buildnumber = -join("v",$versionNumber ,"_","$(Build.BuildNumber)")
And also update
Write-Host "##vso[build.updatebuildnumber]$buildnumber"
You can then use the varialbe $(Build.BuildNumber) on release pipeline

GitHub Actions Terraform State file cant be parsed

I am currently using terraform to provision Infrastructure in the cloud. On Top of Terraform I run GitHub Actions to automate even more Steps.
In this case, after provisioning the infrastructure I generate an inventory file (for ansible) with a bash script using the generated cluster.tfstate to parse names and ips.
However the Script cant run, as it throws following error
Run bash ./generate-inventory.sh cluster.tfstate > ../hosts.ini
parse error: Invalid numeric literal at line 1, column 9
Error: Process completed with exit code 4.
Running it locally however works. When i do a cat on the cluster.tfstate inside the workflow the following is the case
Run cat cluster.tfstate
***
"version": 4,
"terraform_version": "1.0.1",
"serial": 386,
"lineage": "3d16a659-b093-551c-b3ab-a1cf8aa5031c",
"outputs": ***
"master_ip_addresses": ***
"value": ***
Does GitHub Actions modify the json that is evaluated by my script because of Secrets i have created? Or are the stars only in the output in the shell?
The Code of the workflow can be seen here https://github.com/eco-bench/eco-bench/blob/main/.github/workflows/terraform.yml
Thanks!
Following did the trick
source "local_file" "AnsibleInventory" {
content = templatefile("inventory.tmpl",
{
worker = {
for key, instance in google_compute_instance.worker :
instance.name => instance.network_interface.0.access_config.0.nat_ip
}
master = {
for key, instance in google_compute_instance.master :
instance.name => instance.network_interface.0.access_config.0.nat_ip
}
}
)
filename = "./inventory.ini"
}
The Template looks like this
[all:vars]
ansible_connection=ssh
ansible_user=lucas
[cloud]
%{ for ip in master ~}
${name} ${ip}
%{ endfor ~}
[edge]
%{ for ip in worker ~}
${ip}
%{ endfor ~}
[cloud:vars]
kubernetes_role=master
[edge:vars]
kubernetes_role=edge

How to pull the right keys and values from jq into an array in bash shell script

I have a json file that is formatted like so:
{
"ServerName1": {
"localip": "192.168.1.1",
"hostname": "server1"
},
"ServerName2": {
"localip": "192.168.1.2",
"hostname": "server2"
},
"ServerName3": {
"localip": "192.168.1.3",
"hostname": "server3"
}
}
And i am trying to write a shell script that uses Dialog to create a menu to run an ssh connection command. I'm parsing with jq, but can't get past the first object level. We have a lot of servers and this will make connecting to them a lot easier. I have the Dialog statement working fine with static data, but we are trying to populate it with a json file with the rest of the data. So i am killing myself trying to figure out how to get just the localip and hostname either into an array to loop into the Dialog command or something that will effectively do the same thing and al I get it it to do so far is spit out
Servername1 = {"localip":"192.168.1.1","hostname":"server1"}
on each line. I'm a shell script newbie but this is messing with sanity now.
This is the jq command that I've been working with so far:
jq -r "to_entries|map(\"\(.key)=\(.value|tostring)\")|.[]" config.json
This is the Dialog command that works well with static data:
callssh(){
clear
ssh $1#$2
}
## Display Menu ##
dialog --clear --title "SSH Relayer"\
--menu "Please choose which server \n\
with which you would like to connect" 15 50 4 \
"Server 1" "192.168.1.1"\
"Server 2" "192.168.1.2"\
"Server 3" "192.168.1.3"\
Exit "Exit to shell" 2>"${INPUT}"
menuitem=$(<"${INPUT}")
case $menuitem in
"Server 1") callssh $sshuser 192.168.1.1;;
"Server 2") callssh $sshuser 192.168.1.2;;
"Server 3") callssh $sshuser 192.168.1.3;;
Exit) clear
echo "Bye!";;
esac
Thanks for any help or pointing in the right direction.
To create a bash array mapping hostnames to ip addresses based on config.json:
declare -A ip_of
# Emit lines of the form:
# hostname localip (without quotation marks)
function hostname_ip {
local json="$1"
jq -r '.[] | "\(.hostname) \(.localip)"' "$json"
}
while read -r hostname ip ; do
ip_of["$hostname"]="$ip"
done < <(hostname_ip config.json)
You can loop through this bash array like so:
for hostname in "${!ip_of[#]}" ; do
echo hostname=$hostname "=>" ${ip_of[$hostname]}
done
For example, assuming the "dialog" presents the hostnames,
you can replace the case statement by:
callssh "$sshuser" "${ip_of[$menuitem]}"

Pass adminPassword Parameter from JSON template to PS script Switch

I'm building a JSON template that deploys a VM in Azure and executing a PowerShell script via Custom Script Extension (CSE). The JSON template was taken from here with some modifications for my company needs.
One of the parameters in the JSON template is adminPassword, that configures the password for the VM's local admin account.
The PowerShell script should deploy a domain controller on the VM. This is the important part of the PS script:
Install-ADDSForest -CreateDnsDelegation:$false -DatabasePath C:\Windows\NTDS -DomainMode 7 -DomainName Domain.local -DomainNetbiosName Domain -ForestMode 7 -InstallDns:$true -LogPath C:\Windows\NTDS -SysvolPath C:\Windows\SYSVOL -NoRebootOnCompletion:$false -Force:$true
The Install-ADDSForest command requires the switch -SafeModeAdministratorPassword for the command to run.
Adding the password as plain text at the beginning of the PS script works, but plain text password is not an option. This is how I tested:
$SafePassPlain = 'Password'
$SafePass = ConvertTo-SecureString -string $SafePassPlain `
-AsPlainText -force
And entering this in the Install-ADDSForest line: -SafeModeAdministratorPassword $SafePass
This is the part in the JSON template where the script runs:
"properties": {
"publisher": "Microsoft.Compute",
"type": "CustomScriptExtension",
"typeHandlerVersion": "1.4",
"autoUpgradeMinorVersion": true,
"settings": {
"fileUris": [
"https://URLtoFile/DC-Domain.ps1
],
"commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -File DC-Domain.ps1"
I would like to pass the adminPassword parameter from the JSON template to the PS script so it will use it for the -SafeModeAdministratorPassword switch.
Is it possible?
I read about ConvertFrom-Json and checked these: 1 2, but I'm not sure how to implement that on my end...
After checking this and this, seeing examples of passing parameters from a JSON template to a PS script, I tried implementing it like this, which didn't work:
"commandToExecute": "[concat('powershell -ExecutionPolicy Unrestricted -File DC-Domain1.ps1 -SafeModeAdministratorPassword ',parameters('adminPassword'))]"
Any help will be appreciated...

Shell script function with global variable

yesterday I got a very easy task, but unfortunatelly looks like i can't do with a nice code.
The task briefly: I have a lot of parameters, that I want to ask with whiptail "interactive" mode in the installer script.
The detail of code:
#!/bin/bash
address="192.168.0.1" # default address, what the user can modify
addressT="" # temporary variable, that I want to check and modify, thats why I don't modify in the function the original variable
port="1234"
portT=""
... #there is a lot of other variable that I need for the installer
function parameter_set {
$1=$(whiptail --title "Installer" --inputbox "$2" 12 60 "$3" 3>&1 1>&2 2>&3) # thats the line 38
}
parameter_set "addressT" "Please enter IP address!" "$address"
parameter_set "portT" "Please enter PORT!" "$port"
But i got the following error:
"./install.sh: line: 38: address=127.0.0.1: command not found"
If I modify the variable to another (not a parameter of function), works well.
function parameter_set {
foobar=$(whiptail --title "Installer" --inputbox "$2" 12 60 "$3" 3>&1 1>&2 2>&3)
echo $foobar
}
I try to use global retval variable, and assign to outside of the function to the original variable, it works, but I think it's not the nicest solution for this task.
Could anybody help me, what I do it wrong? :)
Thanks in advance (and sorry for my bad english..),
Attila
It seems that your whiptail command is not producing anyoutput because of the redirections. So the command substitution leaves the value empty. Try removing them. Also it's better to save the new value to a local variable first:
parameter_set() {
local NAME=$1
local NEWVALUE=$(whiptail --title "Installer" --inputbox "$2" 12 60 "$3")
export $NAME="$NEWVALUE"
}