Unable to run command on Batch File - batch-file

I have a batch file to open directories on Windows Terminal.
Currently there are 2 lines on my script:
cmd.exe /c wt.exe --window 0 new-tab --profile "RAN" --title "Components" --tabColor "#8AD8FF" --colorScheme "One Half Dark" -d "C:\RAN\components-page"
And have a one for multiple panes:
cmd.exe /c wt.exe --window 0 new-tab --profile "RAN" --title "Address" --tabColor "#9C37FF" --colorScheme "One Half Dark" -d "C:\RAN\address-page" ; split-pane -V -p "RAN" --title "Address Info" --tabColor "#9C37FF" -d "C:\RAN\address-info-page"
The thing is that it does work to open I get a page like this:
What the command loads
What I would like to do is simple to have that but in the same script make it start my pages, by doing by running the command yarn start:local in that directory, as currently I can only do it manually.
Is there a way to do this?
I have added at the end of the -d (directory address) a && yarn start:local.START but hasn't work, no other online info I tried has work so far.

Seeing that you are using powershell in Windows Terminal, you can just add at the end: powershell -NoExit "yarn start:local". So you're end command would be something like:
cmd.exe /c wt.exe --window 0 new-tab --profile "RAN" --title "Components" --tabColor "#8AD8FF" --colorScheme "One Half Dark" -d "C:\RAN\components-page" powershell -NoExit yarn start:local

Related

How to trigger shell script in Unix Server using PSCP Commands? [duplicate]

I need to execute a shell script remotely inside the Linux box from Windows
#!/bin/bash
if [ "$#" -ne 1 ]; then
echo "Illegal number of parameters"
exit
fi
echo "$1"
Here is the command I ran from Windows command prompt
cmd> plink.exe -ssh username#host -pw gbG32s4D/ -m C:\myscript.sh 5
I am getting output as
"Illegal number of parameters"
Is there any way I can pass command line parameter to shell script which will execute on remote server?
You misunderstand how the -m switch works.
It is just a way to make plink load the commands to send to the server from a local file.
The file is NOT uploaded and executed on the remote server (with arguments).
It's contents is read locally and sent to the server and executed there as if you typed it on a (remote) command line. You cannot give it arguments.
A workaround is to generate the file on the fly locally before running plink from a batch file (say run.bat):
echo echo %1 > script.tmp
plink.exe -ssh username#host -pw gbG32s4D/ -m script.tmp
Then run the batch file with the argument:
run.bat 5
The above will make the script execute echo 5 on the server.
If the script is complex, instead of assembling it locally, have it ready on the server (as #MarcelKuiper suggested) and execute just the script via Plink.
plink.exe -ssh username#host -pw gbG32s4D/ "./myscript.sh %1"
In this case, as we execute just one command, you can pass it on Plink command line, including the arguments. You do not have to use the -m switch with a (temporary) file.
I triggered the Shell script in "commands.txt" from Plink which worked for me like a charm with below method I tried:
You can define your script as an one liner using && in a file (I defined in one liner)
You need to run your command in <
Note: Use first EOF in quote like <<'EOF' but not the last one. Else you will see you code will behave weirdly.
Please see below.
Example:
sudo -i <<'EOF'
<your script here>
EOF
Then, finally run it using Plink:
plink -ssh username#hostname -pw password -m commands.txt
Have you tried putting the command and argument in quotes:
i.e. -m "C:\myscript.sh 5"

PuTTY -m command option failing with "unable to open ... file" on Windows 7

I'm trying to make an automatic PuTTY login using a batch file. I have this:
start "title" "C:\Program Files\PuTTY\putty.exe" "server_name#server_IP" -pw "password" -m "commands_to_execute.txt"
Everything works on my Windows 10 machine but in Windows 7, the command option -m does not work. The error message is:
unable to open command file:"commands_to_execute.txt"
I have tried changing all paths to "C:\Program Files\PuTTY", setting the working directory /D, working in the actual directory and I also add the path to the enviroment variables in:
Advance system settings >Enviroment Variables
I have also used plink instead of putty.
What is happening?
It is very unlikely that your problem has anything to do with Windows 7 vs Windows 10.
Most likely the working directory for your batch file execution on Windows 7 is not set to the folder, where the commands_to_execute.txt file is stored.
Possible solutions are:
Set the working directory the same way you have set it on Windows 10
Use a full path to the script file:
-m "C:\path\to\commands_to_execute.txt"
Set working directory for PuTTY explicitly using:
start "title" /D "C:\path\to" "C:\Program Files\PuTTY\putty.exe" ...
Or, if the script file is in the same folder as your batch file, you can use:
start "title" /D "%~dp0" "C:\Program Files\PuTTY\putty.exe" ...
you need to use plink.exe for this not putty.exe, just replace:
start "title" "C:\Program Files\PuTTY\plink.exe" "server_name#server_IP" -pw "password" -m "commands_to_execute.txt"
or make it even easier:
cd C:\Program Files\PuTTY\
plink.exe -ssh pi#192.168.1.166 -P 22 -pw P#SSWRD ~/script.sh
plink.exe -ssh pi#192.168.1.166 -P 22 -pw P#SSWRD -m commands.txt
pause
either of the two lines work.

Save CMD log to file

Is there any way to save all the console output from command.exe or powershell to a file at the end of a session rather than piping each individual output?
I'm trying to save the output of an application that I run from a batch script that crashes when redirected but works fine when printing to the terminal.
This is okay; all output prints to console:
C:\TestPlatform\executables> TestApp.exe -c off -d file -q otp 47f64
All of the below produce no output; Program returns after 1-2 seconds instead of the 15-20 it should:
PS C:\TestPlatform\executables> TestApp.exe -c off -d file -q otp 47f64 | tee LogFile.txt
PS C:\TestPlatform\executables> TestApp.exe -c off -d file -q otp 47f64 | out-file -append LogFile.txt
C:\TestPlatform\executables> TestApp.exe -c off -d file -q otp 47f64 > logFile.txt 2>&1
I couldn't reproduce however it is possible that redirection doesn't work for your exe because all text after the last switch is considered as arg to that switch so the exe consumes everything on the line till the last character - this would explain also why it doesn't run normally.
Try this alternative of logging in cmd:
C:\TestPlatform\executables>> logFile.txt 2>&1 TestApp.exe -c off -d file -q otp 47f64
Note the redirection character right after the prompt.

Piping output in cmd batch file on Azure Batch service error codes not detected

I have a .cmd batch file (let's call it RunSQlCmd.cmd) which pipes out output of sqlcmd to 7 zip compressor
sqlcmd -i.\table.sql -S . -E -s "," -I -h -1 -W| "c:\Program Files\7-Zip\7z.exe" a -tbzip2 -si "out.csv.bz2"
I run it from Azure Batch in a task from a C# driver program with the following command line
(1) cmd /c %AZ_BATCH_NODE_SHARED_DIR%\RunSqlCmd.cmd
But what seem to happen is that command line returns almost immediately and commands in RunSqlCmd.cmd are not fully executed and empty compressed archive is created. Azure Batch task exits with success code 0.
If I change task's command line to
(2) cmd /c start /wait %AZ_BATCH_NODE_SHARED_DIR%\RunSqlCmd.cmd
commands in a batch file run successfully but stdout and stderr is lost for Azure Batch as batch file runs in a separate cmd window and task hangs up without getting any error code.
changing tasks's command line to
(3) cmd /c start /B /wait %AZ_BATCH_NODE_SHARED_DIR%\RunSqlCmd.cmd
is similar to (1)
What is the correct way to do it so that Azure Batch detects correctly when task is finished and RunSqlCmd.cmd command finishes completely?
P.S. Real content of RunSqlCmd.cmd file is as
#echo Run SqlCmd
sqlcmd -i "%~dp0%1.sql" -d dbName -S serverName -U userName -P "password" -s "," -I -h -1 -W -b | "%ProgramFiles%\7-Zip\7z.exe" a -tbzip2 -si "%~dp0%1.csv.bz2"
#echo Done SqlCmd
It takes 1 parameter - name of sql file to pull data
I do not quite understand why you want to run cmd /c and start /wait to kick off a program. you do not need to call cmd.exe from start and do not need to call cmd to start a batch file.
if it is a batch file, you can just call the batch file. it will automatically wait internally for the command to complete.
%AZ_BATCH_NODE_SHARED_DIR%\RunSqlCmd.cmd
That should work as is without calling start or cmd again.
The main problem is that you are sending the command with parameters in quotes to external program, so you you call cmd again and it sends the commands to this cmd window and stripping out quotes when it sends to the external. Just calling the script as is will do.

Redirecting an output of Plink produces an empty file

I'm programming a short batch file that opens plink and redirects the output to a log file. But my log file is empty. Any advice please?
start plink.exe -serial %COM_DEVICE% -sercfg xxxxx,8,n,1,N -v > %CD%\log\tmpLog.log
The start opens the command (the plink.exe) in a new console.
The redirection redirects an output of the start, which is none.
It does not look like you actually need the start command for anything. Remove it:
plink.exe -serial %COM_DEVICE% -sercfg xxxxx,8,n,1,N -v > %CD%\log\tmpLog.log
If you really need to use the START command to spawn it to another process because you want your batch file to continue to run then use cmd.exe to run the process.
start "" cmd /c "plink.exe -serial %COM_DEVICE% -sercfg xxxxx,8,n,1,N -v > %CD%\log\tmpLog.log"
after modification as below:
plink.exe -serial %COM_DEVICE% -sercfg 19200,8,n,1,N -v > %CD%\log\tmpLog.log 2>&1
I removed start and add 2>&1
it's working, thank yall

Resources