Save CMD log to file - batch-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.

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"

Whitespace differences with output

I have written a script to generate a .CSV file from an SQLCMD query, but when I open it in Notepad, there is unwanted spacing between data. When I manually paste the query results in the .csv file then there is no spacing when I open it in Notepad.
Please do let me know, what should I do to avoid the issue.
My minimal script example:
set destfolder="c:\Test"
SQLCMD -s "--Database name--" -d Audit -E -I -i "C:\Test\Mandates.sql" -s "," -o "c:\Test\tempfile.csv"
findstr /v /c:"---" "c:\Test\tempfile.csv" > "%destfolder%\%filename%"
del "%destfolder%\tempfile.csv"
Issue:
CustomerName,CustomerNumber,Value
Adam, 123456789, 0
Expected:
CustomerName,CustomerNumber,Value
Adam,123456789,0
I got the solution.
SQLCMD -s "--Database name--" -d Audit -E -I -W -i "C:\Test\Mandates.sql" -s "," -o "c:\Test\tempfile.csv"
I have used -W in the above script which resolves the issue. Thanks

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

Strip unneeded info from the exported CSV file

I'm using this batch script to export the result of SQL-script in a tab-delimited CSV.
sqlcmd -S PRISHA-CRM -i "C:\Users\Administrator\Desktop\test.sql" -U essl1 -P essl -o C:\outputfile.csv -s " "
How to prevent the second row ---------- from appearing in the exported file?
How to prevent the text in B4 cell from appearing in the exported file?
Tried multiple things but it's not working.
If you want to remove the dashed lines then pipe your output to the FINDSTR command and then redirect to the output file.
sqlcmd -S PRISHA-CRM -i "C:\Users\Administrator\Desktop\test.sql" -U essl1 -P essl -s " " | findstr /R /C:"^[^-]*$" >C:\outputfile.csv
Don't know anything about SQLCMD so I don't know why you are getting data in B4.

running a bunch of SQL files in a folder

I have a bunch of scripts in a folder which I have to run in order. I just open it up in SSMS and hit execute one by one. I have to wait for one of them to complete then run the second one.
Is there a way for me to create a batch script or a script in SSMS (if its possible) that will go through the files one by one? This would save me time as I can do something else while the scripts are running and prevent error (most of them rely on temp tables created in succession).
I think a batch script with sqlcmd is what I am looking for but no idea how to accomplish this task.
Thank you.
Very easy to put together a batch file for this. Put something like the following into a text file but end the file with .bat, eg 'ExecuteMyScripts.bat'
Sqlcmd takes various arguments but the main ones are -S for server instance, -d for database, -i for input file ie your SQL file, -U for user, -P for password, and -o for output file. Watch the case of the letters.
So open notepad and add the following:
sqlcmd -S mysqlserver -d mydb -U sa -P pass1 -i "c:\script1.sql" -o "c:\script1log.txt"
IF ERRORLEVEL 1 goto :eof
sqlcmd -S mysqlserver -d mydb -U sa -P pass1 -i "c:\script2.sql" -o "c:\script2log.txt"
IF ERRORLEVEL 1 goto :eof
sqlcmd -S mysqlserver -d mydb -U sa -P pass1 -i "c:\scripr3.sql" -o "c:\script3log.txt"
Save the file as ExecuteMyScripts.bat.
Double click the file and both script will be executed.
Adding 'IF ERRORLEVEL 1 goto :eof' after each sqlcmd call will jump to the end of the script if error found.
Full usage found here, http://msdn.microsoft.com/en-us/library/ms162773.aspx

Resources