Question

I'm trying to transfer a file onto a remote computer with a batch file. I don't have permissions to use xcopy or copy. I can't use something like psexec for security reasons. Just echoing with \servername\filelocation won't work either due to permissions. The only way so far I have found is to do a for loop over the file and use a wmic call to echo each line out on the remote computer, but for some reason this isn't working. I get duplicate lines and others don't show up at all (I think this is due to special characters). I thought it might have due to timing so I added a one second wait after each wmic call. It was a slight improvement but still doesn't work. Any other ways to do it or ideas as to why wmic causes duplicates?

Code from the comment:

for /F "tokens=*" %%H in (Filename) do (
    wmic /user:%user% /password:%pass% /node:%node% process call create "cmd.exe /c echo %%H >> Location"
  ) & (Timeout /T 2
)
Was it helpful?

Solution

After a test I have to revise my assumption, the order seems never be changed.
As you start one process per line and each process is independent, I assume that they are able to run in any order.

Many of them will be processed in your expected order, but not all.

The major problem are the special characters and also commas and quotes. The special characters can be escaped with a simple caret, but a comma seems immune against any escaping.
Also linefeeds seems unescapable.
This seems a side effect of the wmic process call syntax.

It can be solved with a helper batch, that can also be created via wmic.

set "recv=(echo set param=%%*& echo(& echo(& echo setlocal EnableDelayedExpansion& echo set param=!param:\n=^^& echo(& echo(!& echo echo(!param!^>^>c:\temp\new.bat) "
setlocal EnableDelayedExpansion
wmic /node:%server% process call create "cmd.exe /c !recv!> c:\temp\recv.bat"
endlocal
timeout /t 2

This produces a batch file named recv.bat

set param=%*


setlocal EnableDelayedExpansion
set param=!param:\n=^

!
echo(!param!>>c:\temp\new.bat

Which can accept one parameter and split it into multiple lines.

recv.bat Line1\nLine2
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top