Failure of WshShellExec.StdOut.ReadAll in MM

Post a reply

Smilies
:D :) :( :o :-? 8) :lol: :x :P :oops: :cry: :evil: :roll: :wink:

BBCode is ON
[img] is ON
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Failure of WshShellExec.StdOut.ReadAll in MM

Re: Failure of WshShellExec.StdOut.ReadAll in MM

by chrisjj » Sat Oct 22, 2016 12:36 pm

Thanks. Unfortunately those solutions fail here, because they need the Wscript object which is unavailable in MM scripts. e.g. this solution http://web.archive.org/web/201610221725 ... =1%2383650 fails, when run direct in an auto script:

Image

or inside the runner you proposed at https://archive.is/VImY8#selection-813.0-813.19 :

Image

Re: Failure of WshShellExec.StdOut.ReadAll in MM

by chrisjj » Sat Oct 22, 2016 7:17 am

rivorson wrote:If it works for cmd but not for ping then the only thing I can think of is that something else is reading the StdOut before your script gets to it, or possibly that the StdOut for the ping command is being redirected. Try reading the StdOut more immediately rather than waiting for the command to finish.
That code (http://pastebin.com/aRPkRj8Z) shows the same fail:

Image (linecount is now unfilled) then Image
rivorson wrote:Alternatively try the Run command as Peke suggests. Although it doesn't give direct access to the StdOut you can redirect the output to a file and then read from the file.
StdOut to file is what I'm using in production at the moment, but since I now want to move to parallel processes, that will require messy recoding for multiple output files (and for StdErr too) which hoping to avoid it by capturing output from WshShellExec. Thanks for the suggestion though. It remains my fallback option.

Re: Failure of WshShellExec.StdOut.ReadAll in MM

by Peke » Fri Oct 21, 2016 8:03 am

Re: Failure of WshShellExec.StdOut.ReadAll in MM

by rivorson » Fri Oct 21, 2016 7:01 am

If it works for cmd but not for ping then the only thing I can think of is that something else is reading the StdOut before your script gets to it, or possibly that the StdOut for the ping command is being redirected. Try reading the StdOut more immediately rather than waiting for the command to finish.

Code: Select all

SUB CJtest

DIM strCommand, WshShell, WshShellExec, strOutput

Const WshFinished = 1
Const WshFailed = 2
strCommand = "ping.exe 127.0.0.1"

Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec(strCommand)

On Error Resume Next
Dim line, linecount
WHILE WshShellExec.Status = 0
	line = vbNullString
	line = WshShellExec.StdOut.ReadLine()
	If line <> vbNullString Then
	    strOutput = strOutput & line & vbCrLf
		linecount = linecount + 1
	End If
WEND

Select Case WshShellExec.Status
    Case WshFinished
        Do
            line = WshShellExec.StdOut.ReadLine()
            If line <> vbNullString Then
				strOutput = strOutput & line & vbCrLf
				linecount = linecount + 1
			End If
        Loop While Not WshShellExec.Stdout.atEndOfStream
    Case WshFailed
        strOutput = WshShellExec.StdErr.ReadAll
End Select

MsgBox strOutput

MsgBox linecount

END SUB
Alternatively try the Run command as Peke suggests. Although it doesn't give direct access to the StdOut you can redirect the output to a file and then read from the file.
https://stackoverflow.com/questions/569 ... ing-output

Re: Failure of WshShellExec.StdOut.ReadAll in MM

by chrisjj » Fri Oct 21, 2016 6:22 am

Further info: With the test command changed to cmd /c dir, there's no fail. Note added to OP.

Re: Failure of WshShellExec.StdOut.ReadAll in MM

by chrisjj » Fri Oct 21, 2016 5:49 am

chrisjj wrote:Thanks. With some necessary adjustment:

Code: Select all

SUB CJtest

DIM strCommand, WshShell, WshShellExec, strOutput

Const WshFinished = 1
Const WshFailed = 2
strCommand = "ping.exe 127.0.0.1"

Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec(strCommand)

WHILE WshShellExec.Status = 0
WEND 

Select Case WshShellExec.Status
   Case WshFinished
       strOutput = WshShellExec.StdOut.ReadAll
   Case WshFailed
       strOutput = WshShellExec.StdErr.ReadAll
End Select

strOutput = ""
Dim line, linecount
Do
    line = WshShellExec.StdOut.ReadLine()
    strOutput = strOutput & line & vbcrlf
    linecount = linecount+1
Loop While Not WshShellExec.Stdout.atEndOfStream

MsgBox strOutput

MsgBox linecount

END SUB
I get this

Image

then this

Image
rivorson wrote:My MM is also 1813 but I don't think MM is the problem. Seems more likely to me that there's something causing the Windows Scripting Host to return the empty string.
When I take that code out of the SUB and give it to cscript.exe or wscript.exe, I get the same.
Whops. Please ignore that. I inserted your added code WITHOUT removing the ReadAll, so the results are invalid.

Inserting it correctly:

Code: Select all

SUB CJtest

DIM strCommand, WshShell, WshShellExec, strOutput

Const WshFinished = 1
Const WshFailed = 2
strCommand = "ping.exe 127.0.0.1"

Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec(strCommand)

WHILE WshShellExec.Status = 0
WEND 

Select Case WshShellExec.Status
   Case WshFinished
			strOutput = ""
			Dim line, linecount
			Do
					line = WshShellExec.StdOut.ReadLine()
					strOutput = strOutput & line & vbcrlf
					linecount = linecount+1
			Loop While Not WshShellExec.Stdout.atEndOfStream
   Case WshFailed
       strOutput = WshShellExec.StdErr.ReadAll
End Select

MsgBox strOutput

MsgBox linecount

END SUB
I get the same bad result in MM

Image Image

but a good result in cscript and wscript:

Image Image

I take this to confirm that the problem is specific to MM, and is not specific to ReadAll.

Re: Failure of WshShellExec.StdOut.ReadAll in MM

by chrisjj » Fri Oct 21, 2016 5:35 am

Peke wrote:Just wondering if anyone tried RUN instead of exec
Run doesn't return an object, so provides no access to StdOut. (Literally replacing Exec with Run fails with "Error #424 - Microsoft VBScript runtime error
Object required: 'WshShell.Run(...)'".
Peke wrote:and what echo report back?
I don't understand this. Could you clarify?

Re: Failure of WshShellExec.StdOut.ReadAll in MM

by Peke » Thu Oct 20, 2016 9:30 pm

Just wondering if anyone tried RUN instead of exec and what echo report back?

Re: Failure of WshShellExec.StdOut.ReadAll in MM

by chrisjj » Thu Oct 20, 2016 8:15 pm

rivorson wrote:The only thing left that I can suggest is creating a fresh portable install of MM to see if it does the same.
"When I take that code out of the SUB and give it to cscript.exe or wscript.exe, I get the same." suggest you were right in suggesting this is not down to MM.

Or does cscript.exe or wscript.exe give different there?

Re: Failure of WshShellExec.StdOut.ReadAll in MM

by rivorson » Thu Oct 20, 2016 7:23 pm

So the WshShellExec.Stdout just isn't giving any result.
The only thing left that I can suggest is creating a fresh portable install of MM to see if it does the same. You could also try the portable install on another computer if you have access to one to see if the problem is specific to your computer.

Re: Failure of WshShellExec.StdOut.ReadAll in MM

by chrisjj » Thu Oct 20, 2016 5:29 pm

Thanks. With some necessary adjustment:

Code: Select all

SUB CJtest

DIM strCommand, WshShell, WshShellExec, strOutput

Const WshFinished = 1
Const WshFailed = 2
strCommand = "ping.exe 127.0.0.1"

Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec(strCommand)

WHILE WshShellExec.Status = 0
WEND 

Select Case WshShellExec.Status
   Case WshFinished
       strOutput = WshShellExec.StdOut.ReadAll
   Case WshFailed
       strOutput = WshShellExec.StdErr.ReadAll
End Select

strOutput = ""
Dim line, linecount
Do
    line = WshShellExec.StdOut.ReadLine()
    strOutput = strOutput & line & vbcrlf
    linecount = linecount+1
Loop While Not WshShellExec.Stdout.atEndOfStream

MsgBox strOutput

MsgBox linecount

END SUB
I get this

Image

then this

Image
rivorson wrote:My MM is also 1813 but I don't think MM is the problem. Seems more likely to me that there's something causing the Windows Scripting Host to return the empty string.
When I take that code out of the SUB and give it to cscript.exe or wscript.exe, I get the same.

Re: Failure of WshShellExec.StdOut.ReadAll in MM

by rivorson » Thu Oct 20, 2016 1:29 pm

My MM is also 1813 but I don't think MM is the problem. Seems more likely to me that there's something causing the Windows Scripting Host to return the empty string.

Have you tried reading the output line by line instead of ReadAll?

Code: Select all

Dim line, linecount
Do
    line = objExec.StdOut.ReadLine()
    strOutput = strOutput & line & vbcrlf
    linecount = linecount+1
Loop While Not WshShellExec.Stdout.atEndOfStream

MsgBox strOutput
MsgBox linecount
I put the linecount in for extra diagnosis so you can see if you get a single empty line or multiple empty lines.

Re: Failure of WshShellExec.StdOut.ReadAll in MM

by chrisjj » Thu Oct 20, 2016 12:39 pm

rivorson wrote:I executed mine from the MM script menu, same as yours but I don't have a keyboard shortcut assigned.
Thanks.

Here with key assignment removed, the problem remains.

Likewise with ScriptType changed to 0.
rivorson wrote:I'm running Windows 10 Pro 64 bit. It could possibly be something in the wscript.exe version, found in C:\Windows\System32. I have version 5.812.10240.16384. I don't think the version would make a difference for this particular command but I can't think of anything else to try.
I have 5.8.7600.16385. On Windows 7 64-bit. MM build is latest - 1813.

What's your MM build?

Re: Failure of WshShellExec.StdOut.ReadAll in MM

by rivorson » Thu Oct 20, 2016 6:55 am

I executed mine from the MM script menu, same as yours but I don't have a keyboard shortcut assigned.

I'm running Windows 10 Pro 64 bit. It could possibly be something in the wscript.exe version, found in C:\Windows\System32. I have version 5.812.10240.16384. I don't think the version would make a difference for this particular command but I can't think of anything else to try.

Re: Failure of WshShellExec.StdOut.ReadAll in MM

by chrisjj » Thu Oct 20, 2016 4:58 am

rivorson wrote:I just tried your code and it worked perfectly on my install.
Wow! Good to know. Thanks.
rivorson wrote:Maybe you could try

Code: Select all

MsgBox TypeName(strOutput)
to check that the value being returned is a string. If it returns something other than a string then it could suggest where to look next.
It says "String".
rivorson wrote:Also, do you have any error handling in your script or is the snippet you've posted the entire script?
Entire script.

Image

I execute it via a command key or from a menu command. The result is the same. A zero-length String.

I'd love to know how you are executing it such that it works.

I'm using Win 7 64-bit.

Top