Jump to content

StdOutRead() Question


root
 Share

Recommended Posts

What I want :

- The Program shall delete old Policies(works)

- The Program shall run gpupdate (works)

- The Program shall look for the question to reboot or logoff and send no

and thats the point, the while-wend part first starts when the gpupdate process ends (since it waits for an insert it never ends automatically, and the msgboxes are just appearing when the gpupdate window is closed manually. (also doesnt work with @SW_HIDE)

any ideas ?

#include <Constants.au3>
FileDelete(@WindowsDir & "\security\templates\policies\gpt*.inf")
$foo = Run("gpupdate /force", "", @SW_SHOW, $STDERR_CHILD + $STDOUT_CHILD)

While 1
   $line = StdoutRead($foo)
   MsgBox(0, "STDOUT read:", $line)
   $line2 = StderrRead($foo)
   MsgBox(0, "STDERR read:", $line2)
WEnd
Edited by root
Link to comment
Share on other sites

This code alteration may help.

#include <Constants.au3>

FileDelete(@WindowsDir & "\security\templates\policies\gpt*.inf")

Global $line, $line2
Global $foo = Run("gpupdate /force", "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

Do
   $line &= StdoutRead($foo)
Until @error
MsgBox(0, "STDOUT read:", $line)

Do
    $line2 &= StderrRead($foo)
Until @error
MsgBox(0, "STDERR read:", $line2)

As for the reboot question that I have not seen, you may need to use StdInWrite to manage it which may mean the you may need to peek using StdOutRead to prevent blocking script flow, to get the data as it would be unexpected?

Link to comment
Share on other sites

Here is an example of using StdoutRead to peek for the text that you want to respond to. Mind that the reboot message text is unknown to me.

#include <Constants.au3>

FileDelete(@WindowsDir & "\security\templates\policies\gpt*.inf")

Global $line, $line2
Global $foo = Run("gpupdate /force", "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

While 1
   $line = StdoutRead($foo, 100, True)
   If @error Then ExitLoop
   If StringInStr($line, 'Computer Policy Refresh has completed') Then
       MsgBox(0, '', 'Found: Computer Policy Refresh has completed')
       ExitLoop
   ElseIf StringInStr($line, 'The reboot message that shows...') Then ; Reboot string to read
       StdInWrite($foo, 'y')
       MsgBox(0, "", "Replied with yes")
       ExitLoop
   EndIf
WEnd

Edit:

I have not added the flag for using StdInWrite in the Run function. I will allow you to do this addition.

Edited by MHz
Link to comment
Share on other sites

Thanks for the help but even that didnt help, since its quite similiar to my first try...

the problem is not the if-clause..

i dont get any stdout-text

#include <Constants.au3>

FileDelete(@WindowsDir & "\security\templates\policies\gpt*.inf")

Global $line, $line2
Global $foo = Run("gpupdate /force", "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

$log = FileOpen("gpUpdate.log", 2)

While 1
   $line = StdoutRead($foo, 100, True)
   If @error Then ExitLoop
   FileWrite($log, $line)
WEnd

gpUpdate.log is empty..

i made it that way with psloggedon.exe and stdoutread is working fine..

i have no idea why it isnt working here....

Link to comment
Share on other sites

With this

#include <Constants.au3>

FileDelete(@WindowsDir & "\security\templates\policies\gpt*.inf")

Global $line, $line2
Global $foo = Run("gpupdate /force", "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

$log = FileOpen("gpUpdate.log", 2)

While 1
   $line = StdoutRead($foo, 100, True)
   If @error Then ExitLoop
   If $line <> '' And FileWrite($log, $line) Then
       Exit
   EndIf
WEnd

I get in the log

Refreshing Policy...

User Policy Refresh has completed.

Computer Policy Refresh has completed

Link to comment
Share on other sites

(I've posted this example to parse the stdout and send a command into a console window on another message too, apologise for that.)

To use this for your own scripts just change the cmd filename to what ever your command file name is and change the $mySubstr to the string you seek.

Below is an example of how to use the new StdoutRead, StderrRead and StdinWrite functions now available in AutoIt 3.2.

It fires a cmd file that contains a DIR command and a pause command. It then waits to see when the "Press any key to continue . . ." string is found in the stdout channel and send a carriage return to close the box.

I've used two rolling buffers for the situation if by chance the string is split across two consecutive reads.

;*********************

; test.au3

;*********************

#include <Constants.au3>

Dim $PID

Dim $numChars

Dim $buff

Dim $tmpBuff

Dim $bQuit

Dim $mySubstr

Dim $str1

Dim $str2

$mySubstr = "Press any key to continue . . ."

$bQuit = 0

$PID = Run("C:\test.cmd","C:\",@SW_HIDE,$STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD)

$buff = ""

$tmpBuff = ""

While 1

$numChars = StdoutRead($PID,0,1)

If @error Then ExitLoop

If $numChars > 0 Then

$tmpBuff = StdoutRead($PID,$numChars)

$str1 = $buff & $tmpBuff

If StringInStr($str1,$mySubstr) Then

StdinWrite($PID,@CR)

StdinWrite($PID)

ExitLoop

EndIf

$buff = $tmpBuff

$tmpBuff = ""

EndIf

Sleep(500)

WEnd

While 1

$buff = StderrRead($PID)

If @error Then ExitLoop

MsgBox(0, "STDERR read:", $buff)

Wend

;***********************

; test.cmd

;***********************

DIR /s C:\

PAUSE

;**********************

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...