sriram
sriram

Reputation: 435

Remote Process Execution using Wmi win32_Process - Getting Stdout of Process

Hi i am able to execute a remote process using Wmi and was able to get return Value and Process Id of the Process. Is there any way to get the Output of the Process which was started by Wmi. Eg. If i start an exe which prints something in console will i be able to get those values using this Api. Any help is appreciated.

Upvotes: 7

Views: 2851

Answers (2)

Andy
Andy

Reputation: 444

You can use psexec to execute the program.exe and get the stdout by pipe

p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while p.poll() is None:
    line = p.stdout.readline()
    line = line.strip().decode('gbk')
    if line:
        print('Subprogram output: [{}]'.format(line))
if p.returncode == 0:
    print('Subprogram success')
    return True
else:
    print('Subprogram failed')
    return False

Upvotes: 0

Ben
Ben

Reputation: 35663

You must redirect the output to a file, then read the file across the network.

Use the CMD.EXE /S /C option to do this.

Example command line to run Program.exe:

CMD.EXE /S /C " "c:\path\to\program.exe" "argument1" "argument2"  > "c:\path\to\stdout.txt" 2> "c:\path\to\stderr.txt" "

Then connect to server like this \\servername\c$\path\to\stdout.txt to read the stdout results.

Note: Pay careful attention to the extra quotes around the command to run. These are necessary to ensure the command line is interpreted correctly.

Upvotes: 4

Related Questions