Jump to content

Read output from CMD using PID


Recommended Posts

Hello,

I have process id of CMD and i want to read output from it. i have used StdoutRead but it does not work.

for example: i start CMD without using Autoit, then i get the process ID, then i use Autoit to read the output using pID, but it does not work.

Pls help

thanks

Link to comment
Share on other sites

14 hours ago, Juvigy said:

I lost my crystal ball so if you would post your code and the error or what exactly is not working will help a lot until i find it

#include <Constants.au3>

Local $iPID = InputBox("Input process id of CMD", "Input process id of CMD")
Local $sStdOut = ""
While 1
    $sStdOut &= StdoutRead($iPID)
    ConsoleWrite(StdoutRead($iPID) & @CRLF)
    If @error Then ExitLoop
Wend

Here is the code

Edited by luckyluke
Link to comment
Share on other sites

try this...

Local $iPID = Run('cmd /?', '', @SW_HIDE, 2); $STDOUT_CHILD
If @error Or Not $iPID Then Exit
Local $sStdOut = ""

Do
    Sleep(10)
    $sStdOut &= StdoutRead($iPID)
Until @error

MsgBox(0, '', $sStdOut)

 

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

8 hours ago, ripdad said:

try this...

Local $iPID = Run('cmd /?', '', @SW_HIDE, 2); $STDOUT_CHILD
If @error Or Not $iPID Then Exit
Local $sStdOut = ""

Do
    Sleep(10)
    $sStdOut &= StdoutRead($iPID)
Until @error

MsgBox(0, '', $sStdOut)

Thank you, but the point is i want the script to read from CMD, i dont want it run CMD command and read.

If the script run CMD, it will read the data out, but is there any way i can read it by using process ID?

 

Link to comment
Share on other sites

On 1/26/2017 at 9:14 PM, Juvigy said:
StdoutRead($iPID)

Doesn't work like that - it needs the PID returned from a child process and a RUN command. What exactly do you want to accomplish in more details?

Hi, here is the steps:

1. i start cmd in windows. open task manager to get the process ID of CMD

2. I run autoit script, then put the ID i got from step 1, then the script will need to read output from cmd. for example in cmd i type "help", then the script will read the output from cmd.

Is that ok?

Link to comment
Share on other sites

1 hour ago, luckyluke said:

Hi, here is the steps:

1. i start cmd in windows. open task manager to get the process ID of CMD

2. I run autoit script, then put the ID i got from step 1, then the script will need to read output from cmd. for example in cmd i type "help", then the script will read the output from cmd.

Is that ok?

Why not just run "wmic process list as part of the script.  The command will list detailed information about each process, including PID.  That may allow you to associate the process name to the pid:

$proc = Run ( @ComSpec & " /c wmic process list", @SystemDir, Default, $STDOUT_CHILD )
ProcessWaitClose ( $proc )
$list = StdoutRead ( $proc )
MsgBox ( 1, "Processes", $list )

 

Link to comment
Share on other sites

I told you - it doesnt work like this. You have to start the CMD from the script with run and then it will work. You cant attach to manually started CMD.

I found and old topic where this was implemented , but i cant make it work with the new AutoIt. It is here:

 

Link to comment
Share on other sites

23 hours ago, MattHiggs said:

Why not just run "wmic process list as part of the script.  The command will list detailed information about each process, including PID.  That may allow you to associate the process name to the pid:

$proc = Run ( @ComSpec & " /c wmic process list", @SystemDir, Default, $STDOUT_CHILD )
ProcessWaitClose ( $proc )
$list = StdoutRead ( $proc )
MsgBox ( 1, "Processes", $list )

 

The point is i want to get text from CMD which is run by other program. so we cant start CMD using autoit.

Link to comment
Share on other sites

14 hours ago, Juvigy said:

I told you - it doesnt work like this. You have to start the CMD from the script with run and then it will work. You cant attach to manually started CMD.

I found and old topic where this was implemented , but i cant make it work with the new AutoIt. It is here:

Thank you, Juvigy. That is really help. I checked it and tested, it worked for me. 

The script using clipget function to get information from CMD command. But one small thing is that if a user using copy paste command (by press Ctrl+C) then it will be messed up.

 

Link to comment
Share on other sites

Please stop the quoting, just reply, quotes are meant for parts you want to refer to.

Even weirder is including your answer inside the quote.

Anyway, here's my thought on the way to solve it.

Opt("WinTitleMatchMode", 2) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase
$hCmd = InputBox("Input", "Input title of CMD window"&@crlf&"(doesn't have to be complete)")
If @error Or $hCmd = '' Then Exit
WinActivate($hCmd)
SendKeepActive($hCmd)
$cmdtext2 = ClipGet()
Send( "! es{Enter}" )
$cmdtext = ClipGet()
ClipPut($cmdtext2)
MsgBox(64, '', $cmdtext)

This keeps your copy/paste text, because it copies to variable does it's thing and copies stuff back again, so it won't mess up copy paste.

Edited by careca
Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...