Jump to content

Question regarding StdinWrite and Stdoutread when used with Console apps


mojomatt
 Share

Recommended Posts

Question regarding StdinWrite  and Stdoutread…

Here’s my setup…
I’m opening a powershell session using the AutoIT Run function with the $STDIN_CHILD  and $STDOUT_CHILD optional flags.

I’m then sending a command using the STDINWRITE function to connect to a remote server which is a long distance away and it takes a “long” time to return results.

I’m then trying to read the STDOUT stream.

After this there are other STDINWRITE statements that I need to make so the STREAM hasn’t closed at this point.

My question is this…How do I know for sure when all the data has been returned from the remote server after my first STDINWRITE?

Things I’ve tried and don’t work…
Use a loop which waits for @error to be non-zero.  This doesn’t work because as long as the STREAM is open then @error never is set to non-zero.

Use a loop which waits for @extended to be 0.  This doesn’t work because the remote server sends data back in bursts and I don’t know how much time will pass in-between bursts.

The only thing that is somewhat reliable is utilizing very long sleep statements – like 5 minutes – which really slows down my script and doesn’t ensure success either.

What I really need is a way to tell that control has been returned to the command prompt.  What I mean by this is if you open a command prompt and issue the command DIR C:\ /s which lists all the files and folders on your C: drive you know you can’t issue any more commands until you get a blinking cursor at the command prompt again.  How do I programatically know when I get a blinking cursor again?

Sample code that isn’t 100% foolproof…

This code uses the logic looking for an @error to be non-zero...

#include <constants.au3>
Dim $StrOutput
$StrPowerShellPID = Run('C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe  -command - ',@SystemDir, @SW_HIDE, $STDIN_CHILD + $STDERR_MERGED)
StdinWrite($StrPowerShellPID, "Dir C:\" & @CRLF) ;this is just an example command
; I can't close the STDOUT stream at this point because I need to issue additional commands into the existing STDIN stream later in the script
While 1 ;Wait for EOF as indicated by @error being non-zero
    $StrOutput &= StdoutRead($StrPowerShellPID)
    If @error <> 0 Then ExitLoop
    sleep (500)
WEnd
Consolewrite($StrOutput)

The above code doesn't work because @error will never be set to non-zero since the stream isn't closed.  I can't close the stream because I need to issue additional commands into it.
 

 

This code uses logic looking for an @extended value of 0 indicating no data was returned to the stream...

#include <constants.au3>
Dim $StrOutput
$StrPowerShellPID = Run('C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe   -command - ',@SystemDir, @SW_HIDE, $STDIN_CHILD + $STDERR_MERGED)
StdinWrite($StrPowerShellPID, "Dir ""C:\program files\common files"" -recurse" & @CRLF)
While 1 ;This loop detects when data starts to stream in and then exits the loop
    $StrOutput &= StdoutRead($StrPowerShellPID)
    If @extended > 0 Then ExitLoop
    sleep(1000)
WEnd

While 1 ;Now that data has begun to stream in I wait until I can't read any more data as indicated by 0 bytes read in the value of @extended
    $StrOutput &= StdoutRead($StrPowerShellPID)
    If @extended = 0 Then ExitLoop
;~  sleep (1000) ; this is the only way to get this specific sample to work but this won't work for me because I don't know the time in-between bursts of data coming into the STDIN stream
WEnd
ConsoleWrite($StrOutput) ;If you look at the output you'll see it's not complete because @extended was set to 0 during the second loop

The above code doesn't always work because if the remote server doesn't send any data for several seconds, then the StdoutRead thinks it's done reading when in reality it may not be.

 

Link to comment
Share on other sites

I don't know if powershell allows to concatenate 2 commands on the same line (as allowed in dos prompt by using the & separator), If this is allowed then you could use this "workaround" by issuing a command like this:

StdinWrite($StrPowerShellPID, "Dir ""C:\program files\common files"" -recurse  & TheEnd!" @CRLF)


where TheEnd! is deliberately a non existing command that will generate an error on the StdErr stream, and that error will be generated only after the first part of the command (the correct one) has finished. In this way you could know that your command has finished when you get "TheEnd!" string as first part on the StdErr stream.

of course you should manage also the StdErr stream by using the $STDERR_CHILD flag in the Run command.
You will trap the "TheEnd!" string that will signal you the end of your command. Here is working example in a DOS environment (hope it can work the same way in  powershell?)

#include <Constants.au3>
Local $cmd_Pid, $Output

; execute a permanent DOS prompt with all streams redirected
$cmd_Pid = Run(@ComSpec & " /k", "", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD)

; issue a command + issue an error at the end of the first command
StdinWrite($cmd_Pid, "ping localhost & TheEnd!" & @CRLF)

Do
    $Output &= StdoutRead($cmd_Pid)
Until StringLeft(StderrRead($cmd_Pid), 9) = '"TheEnd!"' ; wait till the deliberately generated error

ConsoleWrite($Output & @CRLF) ; show result of first command
StdinWrite($cmd_Pid, "exit" & @CRLF) ; close the permanent command promp

p.s.

here a link about Running multiple DOS commands in one line https://www.autoitscript.com/forum/topic/54864-run-multiple-dos-commands-in-one-line/

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Wow Chimp that's pretty close to being brilliant!  :D

The semicolon is used to concatenate multiple commands on the same line in Powershell.

Here's my working code:

#include <constants.au3>
Dim $StrOutput
$StrPowerShellPID = Run('C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe   -command - ',@SystemDir, @SW_HIDE, $STDIN_CHILD + $STDERR_MERGED)
StdinWrite($StrPowerShellPID, "Dir ""C:\program files\common files"" -recurse; write-host ""All Done!""" & @CRLF)
While 1 ;This loop detects when data starts to stream in and then exits the loop
    $StrOutput &= StdoutRead($StrPowerShellPID)
    If @extended > 0 Then ExitLoop
    sleep(1000)
WEnd

While 1 ;Now that data has begun to stream in I wait until I see the text "All Done!" which tells me my first command has completed
    $StrOutput &= StdoutRead($StrPowerShellPID)
    If StringInStr($StrOutput, "All Done!") Then ExitLoop
    Sleep (1000)
WEnd
ConsoleWrite($StrOutput)

Thanks a ton for the help!!!

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

×
×
  • Create New...