Use of echo commands in batch script - powershell

I have to convert a batch script to PowerShell. I came across these few lines of code and I have no idea what they do. Could someone explain it, especially the echo statements and robocopy?
set logdir=D:\Internal\Log
set runlog=%logdir%\run.log
set roboexe=robocopy /NJH /XX /NP
echo ^<!-- 1>>%runlog% 2>&1
echo Move-Copy - %account% %Time:/=% 1>>%runlog% 2>&1
echo Found files for %account% IN 1>>%runlog% 2>&1
%roboexe% "%fromdir%" "%historydir%" %wildcard% 1>>%runlog%

echo in shell just prints output to the screen. The command also works in PowerShell, but it's really an alias of Write-Output, which sends output to the standard success stream/pipeline (which if there's nowhere else for it to go, is the console/screen by default).
Robocopy is an advanced copying utility. It has a range of abilities beyond the standard copy function of Windows. It is documented here.
To explain the tokens in this line:
echo ^<!-- 1>>%runlog% 2>&1
<, >, and >> are redirection operators. The ^ character escapes the < character so that it's being used literally rather than as a redirect. !-- are also just being printed literally I believe.
1>> is using the append redirect to send the append the standard output to file specified in %runlog%.
2>&1 redirects the error output stream to the standard output stream, so basically errors would also be written to the runlog.

Related

Avoid Line break at end of cmd output?

when I use this command pwsh -c echo hello in cmd I get the following output:
C:\>pwsh -c echo hello
hello
C:\>
I do not get that line break at the end
when I run it on powershell:
PS C:\> pwsh -c echo hello
hello
PS C:\>
So I think the problem is in cmd. I know this is not such a problem and have an easy fix but I have some programs uses cmd to access powershell and removing that line break is not that fun.
So is there any fix to prevent cmd to add that line ?
Mofi has provided the crucial pointers in comments:
When executing a command interactively, cmd.exe unconditionally appends a a newline (line break) to the command's output, presumably for readability and perhaps also to ensure that the next prompt always starts on a new line.
This applies irrespective of what that command is. In other words: It doesn't matter that your command happens to be a PowerShell command.
However, that trailing newline does not become part of the command's output, therefore programmatic processing of a command's output is not affected, such as when you redirect > to a file or process the output lines one by one with for /f.
In other words: for programmatic processing you need not remove the trailing newline, because it isn't part of the actual command output.
Conversely, if you really need to in effect suppress the trailing newline for display, you'll have to modify the command's output - if that is even an option - so that the output itself doesn't end in a newline, as shown in this SuperUser answer for cmd.exe's own echo command; for PowerShell, you could do pwsh -c Write-Host -NoNewLine hello.
Edge case:
When capturing output from a batch file that is running without #echo off (or with echo on) - in which case the trailing newlines do become part of the output - you can filter out empty lines by piping to findstr /r /v /c:"^$" (as also shown in the linked answer); e.g.
foo.cmd | findstr /r /v /c:"^$"
However, note that all empty lines are filtered out this way - potentially including actual empty lines in the output from commands executed by the batch file.
If preventing that is required, a more sophisticated approach is required, which, however (a) relies on the standard prompt string (e.g., C:\>) being used and (b) can still yield false positives:
foo.cmd | powershell -nop -c "#($Input) -join \"`n\" -replace '\n(?=[a-z]:\\.*?>)'"
Finally note that if you execute the above commands without capturing or redirecting their output, their overall output in the cmd.exe console will again have a trailing newline.

How to write STDOUT/STDIN via RedMon/cmd.exe to file?

I am trying to redirect a PS output to a file and process it further.
For this I am using the Printer Port Redirection RedMon which is sending the output to CMD.exe
C:\Windows\system32\cmd.exe
As arguments I expected that something like the following should work, but it does not. "%1" contains the user input for filename.
/c >"%1"
or
/c 1>"%1"
or
/c |"%1"
or
/c > "%1" 2>&1
What almost works if I send the output to a batch file which writes it then to file.
/c WriteOutput.bat "%1"
However, the batch file is somehow altering the file (skipping empty lines, and ignoring exclamation marks and so on...)
If possible I want to avoid a batch file. Is there a way to get it "directly" to a file?
Select "Print to FILE" in the printer options is not an option for me. I want the same end result but via cmd.exe being able to process it further.
Any ideas?
Edit:
Well, that's the batch file I used. It neglects empty lines and space at the beginning.
#echo off
setlocal
set FileName=%1
echo(>%FileName%.ps
for /F "tokens=*" %%A in ('more') do (
echo %%A>>%FileName%.ps
)
Well, so far I still haven't found a direct way to write STDIN via RedMon via CMD.exe to a file. As #aschipfl wrote, all the versions with for /F will skip lines and ignore certain characters.
However, with the following batch script (via RedMon) I end up with a "correct looking" file on disk.
C:\Windows\system32\cmd.exe /c WritePS.bat "%1"
"%1" contains the user input for filename without extension.
The Batch-File WritePS.bat looks as simple as this:
#echo off & setlocal
set FileName=%1.ps
more > "%FileName%"
However,
the resulting Postscript file is different from a file which I "Print to FILE" via the Postscript-Printer setup. I am pretty sure that all the printer settings which I can set are the same in both cases.
If anybody has an idea why there might be a difference, please let me know.

Cannot pass asterisk character to external command via powershell

I cannot pass an asterisk character to external command using powershell
I use following a line like
& .\args.bat #("-arg1", "-arg2", "*.test.com")
where args.bat just dumps passed arguments
#ECHO off
ECHO The %~nx0 script args are...
for %%I IN (%*) DO ECHO %%I
And instead of passing "*.test.com" it lookups up current directory for files matching pattern "*.test.com" and sends list of them to args.bat
That can be seen if you use "*" instead of "*.test.com"
I've also tried back tick character to escape asterisk, but it didn't help
In my case using asterisk is required as I'm passing it to makecert.exe to create wildcard domain certificate
Please help
I'm not seeing that. As a test I created args.bat like so:
'pause' > args.bat
Then executed it:
& .\args.bat #("-arg1", "-arg2", "*.ps1")
When I look in task manager at the command line for that cmd.exe, I see:
It must be something the batch file is doing because PowerShell is doing nothing to your *. BTW I used *.ps1 because the directory I executed from had a ton of ps1 files in it.
If you are PS 3.0 or higher you can use "stop parsing operator" --%. See help about_parsing for details.

echo with pipe in sh script creates filename with "?"

I'm confronted with a rather strange problem an echo command causes in a script.
It's supposed to be really REALLY basic stuff, but still, there's something "off".
Suppose, I have this script:
#!/bin/bash
# SERVERPID='cat lite_server_pid.txt'
# kill -9 $SERVERPID
nohup java -Xmx3G -Xms2G -jar tekkit_lite_065.jar nogui > output.txt &
echo $! > lite_server_pid.txt
Yes, this starts my own little Minecraft/Tekkit-Server. ;-)
The Problem is, the file thats created is (for some reason) named
lite_server_pid.txt?
and YES, this includes the "?"! Doing the same command in shell, a file without ? is correctly created! Also, the content of the file is the desired processID.
Still, the ? following the filename is a major problem...
What am I doing wrong?
Check your file for DOS line endings. I suspect that ? is actually your terminal's attempt to display a carriage return (\r). Since bash expects UNIX-style newlines, the carriage return part of the DOS newline (\r\n) is treated as a legal character for the file name.
Run your script through dos2unix.

How can I redirect the output of a command (running in a batch loop) to a file?

I have a windows batch file, which iterates over files in a folder and runs a command on each file. Specifically I am running xmllint to validate some files:
for %%i in (c:\temp\*.xml) do (
C:\XMLLINT\xmllint -noout -schema "C:\schemas\schema.xsd" "%%~dpnxi" >> c:\output.txt
)
It currently shows the output on screen. I want to show the output of all these commands placed in an output file. How can I achieve this? By using the append operator (>>) nothing is accomplished except for a blank file being created.
Is it because of xmllint?
If you're trying to redirect error output from the program, it might be writing to stderr. You can try to redirect it with:
for %%i in (c:\temp\*.xml) do (
C:\XMLLINT\xmllint -noout -schema "C:\schemas\schema.xsd" "%%~dpnxi" >> c:\output.txt 2>&1
)
Basically the 2>&1 at the end means redirect anything from stderr (which is 2) to stdout (which is 1). Since stdout is redirected to a file, you should now see the stderr stream in the file. Hope this works for you!
I've never used it, but if its documentation is here, have you tried just removing your "-noout" option, or adding an: "-output c:\output.txt"?