View unanswered posts | View active topics It is currently 18 Dec 2014 17:47



Post new topic Reply to topic  [ 8 posts ] 
Interactive Remote command execution through wmic 
Author Message

Joined: 31 Mar 2012 21:31
Posts: 160
Post Interactive Remote command execution through wmic
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"


18 May 2012 11:48
Profile

Joined: 31 Mar 2012 21:31
Posts: 160
Post Re: Interactive Remote command execution through wmic
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


18 May 2012 12:21
Profile
Expert

Joined: 22 Jan 2010 18:01
Posts: 1807
Location: Germany
Post Re: Interactive Remote command execution through wmic
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:
wmic /node:"S8DCAB1" datafile where name="c:\\file.txt" get status | find "OK"
echo %errorlevel%

Regards
aGerman


18 May 2012 13:50
Profile

Joined: 31 Mar 2012 21:31
Posts: 160
Post Re: Interactive Remote command execution through wmic
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:
@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


18 May 2012 15:08
Profile
Expert

Joined: 22 Jan 2010 18:01
Posts: 1807
Location: Germany
Post Re: Interactive Remote command execution through wmic
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


18 May 2012 16:48
Profile

Joined: 31 Mar 2012 21:31
Posts: 160
Post Re: Interactive Remote command execution through wmic
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.


18 May 2012 17:33
Profile

Joined: 31 Mar 2012 21:31
Posts: 160
Post Re: Interactive Remote command execution through wmic
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:
@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



18 May 2012 21:20
Profile

Joined: 01 Mar 2013 12:31
Posts: 1
Post Re: Interactive Remote command execution through wmic
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.


01 Mar 2013 12:36
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 15 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.