-1

I have come across a certain problem, hopefully some of you can help me with it. Basically say I have computer 1, which executes via wmic a certain batch file (process) in computer 2, let's call it Script.bat. Depending on how the process ends, I want to get a log on computer 1 saying success/failure. For that I thought I could have 2 files on computer 1, Success.bat and Failure.bat, and run wmic again from computer 2 (inside Script.bat), either remotely running Success.bat or Failure.bat in computer 1.

Well, the problem is that if I run Script.bat via cmd in computer 2, process launches and logs get saved in computer 1; however if I run Script.bat via wmic from computer 1, process launches but I don't get logging. Is it because I can't run wmic remotely, and if so what are some alternatives? I know wmic is a little dated but changing everything to say PowerShell is probably too much work.

Thanks in advance

Edit: since apparently more info is needed, this is hopefully clearer. Success.bat and Failure.bat can be as simple as

echo Success>>C:\log_file.txt;

the script in computer 2 is basically as follows:

::Script.bat

C:\foo.exe --start --user admin --password passwd
::This writes an output in my_processes.txt file
::We look for "Custom" in that file to determine the status

findstr /C:"Custom" "my_processes.txt" >nul
if %errorlevel% equ 1 (set /a flag=1)
if %flag% gtr 0 (goto thingsWentWrong)
wmic /node:COMPUTER1 process call create "C:\Success.bat"
exit /b 0

:thingsWentWrong
wmic /node:COMPUTER1 process call create "C:\Failure.bat"
exit /b 1

Now, that script is stored in computer 2, say C:\Script.bat. If it is run "locally" from computer 2 (just by double clicking for example, or through cmd), then Success.bat or Failure.bat are executed in computer 1 just fine and the logs are stored; however if we run

wmic /node:COMPUTER2 process call create "C:\Script.bat"

from computer 1, foo.exe is started correctly, findstr works as intended, but the wmic lines are simply not run (logs in computer 1 are not created). It's as if those lines did not exist.

Apologies for not being clear enough up front.

  • Please rephrase your question. I almost voted to close it as a duplicate, thinking you were asking how to run `wmic` on a remote host. – SomethingDark Jun 13 '23 at 14:30
  • I don't see how my question is unclear, I want to run wmic inside another script that has been launched through wmic. – franjuanele Jun 13 '23 at 14:36
  • Where is your code? We would need to see the content of at least `Script.bat` and your "certain batch file (process)" In order to assist you with a single and specific code problem, we need to be able to replicate your environment, run the code in exactly the same way as you, and reproduce the same problem. Corrently we don't have any of that information, and as such your question is about how to perform a task, not how to fix a particular issue with your submitted code. This site deals with the latter. – Compo Jun 13 '23 at 15:28

1 Answers1

1

Since I have solved my problem since posting the question, I figured it could maybe be helpful to others. First, I couldn't get wmic to run another wmic in a different computer: so calling wmic /node:COMPUTER2 process call create "C:\hello.bat" on computer 1, with hello.bat being just wmic /node:COMPUTER1 process call create "echo hello>C:\hello.txt" (in computer 2) doesn't work (at least for me, could be an issue with user permissions as that has been source of other problems).

My solution was to use waitfor: so in the example above, the script on computer 1 waits for success with waitfor /t 5 Success. (computer 1 waits to receive the message "Success" for up to 5 seconds). If computer 2 succeeds, then it sends the message "Success" with

waitfor -si Success.

If it fails, then computer 1 doesn't receive any message after 5 seconds (note that the findstr task on computer 2 takes very little time, so if after 5 seconds no message has been received, it means there has been some problem). Depending on whether waitfor receives the message or times out, the errorlevel is set to 0 or 1, and we go on from there. For a quick sketch of both scripts:

::Script to be run on computer 1
...
wmic /node:COMPUTER2 process call create "C:\Script.bat"
waitfor /t 5 Success

if %errorlevel% gtr 0 (goto ThingsWentWrong)

:ThingsWentWell
echo Success>>C:\logfile.txt
...

:ThingsWentWrong
echo Failure>>C:\logfile.txt
...
::Script to be run on computer 2 (C:\Script.bat)
C:\foo.exe --start --user admin --password passwd
::This writes an output in my_processes.txt file
::We look for "Custom" in that file to determine the status

findstr /C:"Custom" "my_processes.txt" >nul
if %errorlevel% equ 0 (waitfor -si Success)
exit /b 0

Hope this is of help to someone.