Interactive Remote command execution through wmic

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Interactive Remote command execution through wmic

#1 Post by MKANET » 18 May 2012 11:48

I'm very new to using wmic. I would like to redirect the output of a remotely executed command to my local PC. Currently, I have to write to a temp file located on the remote machine in order to see the result.


wmic /node:S8DCAB1 process call create "cmd.exe /c dir c:\ >C:\Windows\Temp\result.txt"

find "file.txt" \\S8dcab1\C$\Windows\Temp\result.txt
if not errorlevel 1 echo -found file.txt
if errorlevel 1 echo -didn't find file.txt


Ideally, I would like to to do this in one go; where the output is redirected directly to my local find command.

Something like this (if it were to work)
wmic /node:S8DCAB1 process call create "cmd.exe /c dir c:\" | find "file.txt"

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Interactive Remote command execution through wmic

#2 Post by MKANET » 18 May 2012 12:21

In case there's no way to do what I want within wmic, I would like to make something that does it for me; however, I'm still running into a problem... I can't take the result from call :Remoteexec and redirect it to the find command. :(



call :RemoteExec "dir c:\" | find file.txt




REM REMOTE EXECUTION
:RemoteExec
wmic /node:%UserInput% process call create "cmd.exe /c %~1 >C:\Windows\Temp\rexec.tmp"
type \\%UserInput%\C$\Windows\Temp\rexec.tmp
del /q \\%UserInput%\C$\Windows\Temp\rexec.tmp >nul
exit /b

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Interactive Remote command execution through wmic

#3 Post by aGerman » 18 May 2012 13:50

The "call create" creates a cmd process on the remote machine. You can't redirect the output of the remote process to the local cmd process.
Seems you just need to find a specific file. Try:

Code: Select all

wmic /node:"S8DCAB1" datafile where name="c:\\file.txt" get status | find "OK"
echo %errorlevel%

Regards
aGerman

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Interactive Remote command execution through wmic

#4 Post by MKANET » 18 May 2012 15:08

Thanks for the quick reply aGerman. I just used finding a file as an example to make things simpler to explain.

Since I have your attention, maybe you can still help.


I am ultimately trying to to execute commands on remote machines conveniently. I thought psexec would work (System Internals utility), however, I kept getting the below errors even when the syntax was correct
ERROR: Input redirection is not supported, exiting the process immediately.

So, I thought I would make my own psexec replacement revolved around wmic. I am able to use psexec.cmd (code below). However, if a file takes too long to process, I get the error message that the temp file is still being used by the process (temp file is still being written).

I figured out a way to detect when the file is no longer in use. But then to my horror.. I realized I AM GET THE SAME DAMN ERROR I did using the System Internals utility!!!
ERROR: Input redirection is not supported, exiting the process immediately.

aGerman, could you please help me to avoid that error. I don't even know exactly why it's happening. I just want a reasonable solution to return the output of a command I execute on a remote machine; even if the output takes several seconds to finish.

Code for PSexec.cmd

usage syntax Examples:
\\servername command arg arg arg
servername command blah blah blah

Code: Select all

@echo off
FOR /F "tokens=1,*" %%A in ("%*") do (
    set UserInput=%%A
    set command=%%B
)

if %UserInput:~0,2%==\\ (
        set UserInput=%UserInput:~2%
)

wmic /node:%UserInput% process call create "cmd.exe /c %command% >C:\Windows\Temp\rexec.tmp" 1>NUL 2>&1

:tryagain
move \\%UserInput%\C$\Windows\Temp\rexec.tmp \\%UserInput%\C$\Windows\Temp\rexec.tmp 2>NUL >NUL
if %errorlevel%==1 timeout /t 1 >nul &goto tryagain

type \\%UserInput%\C$\Windows\Temp\rexec.tmp
del /q \\%UserInput%\C$\Windows\Temp\rexec.tmp >nul
exit /b

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Interactive Remote command execution through wmic

#5 Post by aGerman » 18 May 2012 16:48

Well I don't have a network and I don't have the permissions to try those on our companys network. All I can test is using my own %USERDOMAIN% where your code works whithout problems.

BTW I can't find an "input redirection" in your code. No idea why the error occurs. Try to run with "echo on" to figure out which line causes the error.

Regards
aGerman

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Interactive Remote command execution through wmic

#6 Post by MKANET » 18 May 2012 17:33

AGerman, I think that error must come from wmi. Maybe Microsoft's Psexec.exe uses wmi as well? I guess it also very important what's being sent to the remote console that needs to be echoed back. One of the commands I send to psexec has a comma in it. It appears that the wmi command gets confused even though the comma is within the two double quotes for the command line it needs to execute.

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Interactive Remote command execution through wmic

#7 Post by MKANET » 18 May 2012 21:20

Edit:

For whatever reasons, there seems to be some limitations using microsoft's psexec.exe and even WMI. The problems may be either related to permissions/timing during redirected output, etc.

My solution.... remote execution with the help of windows scheduler! Command execution is much more reliable and also instant! I also added a -d switch for executing commands that don't need to wait for output. I think this might be the first time I made a useful command line tool that works better than anything I could find available online :)

Same usage syntax; pretty much identical to psexec.exe from MS:

Code: Select all

@echo off
set count=""
set noredirectout=""

FOR /F "tokens=1,*" %%A in ("%*") do (
    set UserInput=%%A
    set command=%%B
)

if %UserInput%==-d (
        FOR /F "tokens=1*" %%A in ("%command%") do (
        set UserInput=%%A
        set command=%%B
        set noredirectout=yes
)
)


if %UserInput:~0,2%==\\ (
        set UserInput=%UserInput:~2%
)



if not %noredirectout%==yes goto redirect

SCHTASKS /S %UserInput% /Create /F /SC DAILY /ST 00:00 /TN "tempTASK" /RU "" /TR "cmd.exe /c %command% >C:\Windows\Temp\rexec.tmp"

>nul
SCHTASKS /END /S %UserInput% /TN "tempTASK" >nul
SCHTASKS /RUN /S %UserInput% /TN "tempTASK" >nul
SCHTASKS /S %UserInput% /TN "tempTASK" /DELETE /F >nul


goto end
:redirect
SCHTASKS /S %UserInput% /Create /F /SC DAILY /ST 00:00 /TN "tempTASK" /RU "" /TR "cmd.exe /c %command% >C:\Windows\Temp\rexec.tmp"

>nul
SCHTASKS /END /S %UserInput% /TN "tempTASK" >nul
SCHTASKS /RUN /S %UserInput% /TN "tempTASK" >nul
SCHTASKS /S %UserInput% /TN "tempTASK" /DELETE /F >nul


:tryagain
move \\%UserInput%\C$\Windows\Temp\rexec.tmp \\%UserInput%\C$\Windows\Temp\rexec.tmp 2>NUL >NUL
if %errorlevel%==1 (
    timeout /t 1 >nul
    set /a count=count+1
    if %count%==20 echo Remote command could not complete &goto end
    goto tryagain
)
type \\%UserInput%\C$\Windows\Temp\rexec.tmp
del /q \\%UserInput%\C$\Windows\Temp\rexec.tmp >nul


:end
exit /b


pschuetze
Posts: 1
Joined: 01 Mar 2013 12:31

Re: Interactive Remote command execution through wmic

#8 Post by pschuetze » 01 Mar 2013 12:36

I have the same error that you had. I turned the echo on and the result see below. The timeout command is complaining. That should be easy to fix, since I only needed a sleep.

E:\....>IF 1 == 1 (timeout /t 5 /nobreak )
ERROR: Input redirection is not supported, exiting the process immediately.

Post Reply