KB-104311: Returning the results of a Windows Script Host (WSH) script.

Question:

How can I return the results of a WSH – Windows Script Host (WScript Object) script fired via Elipse E3? For example: can I retrieve the resulting string of a command executed via Command Prompt?

command prompt

Solution:

Yes, you can return the results of a WSH script fired via Elipse E3. To do so, you must first understand what the Windows Script Host is, and how it works.

The Windows Script Host, also known as “WSH”, is a comprehensive script infrastructure for theMicrosoft® Windows® platform. There are two Windows Script mechanisms available for scripting: Visual Basic® Scripting Edition and Microsoft JScript®. Both these tools allow you to develop different types of scripts in order to accomplish various tasks inside the Windows environment. And since the WSH environment can be expanded, you can also use other programming languages for it, such as Python, for example.

It also includes several automation methods that are compatible with COM platform, thus allowing you to perform taks via the WSH objects model.

WSH script

Therefore, in order to return the string that contains the executed result of the command via Elipse E3 prompt, we will create a  WshShell object, using Exec() method StdOut.ReadAll function:

Sub CommandButton2_Click()
strCMD = "Xcopy ""C:SourceDoc1.txt"" ""E:Destination"" /Y"
Set WshShell = CreateObject("WSCript.shell")
strReturn = WshShell.Exec(strCMD).StdOut.ReadAll
msgbox strReturn
End Sub

returned result

Example with ping command:

return with ping command

Sub CommandButton1_Click()
strCMD = "ping 192.168.8.8" 
set WshShell = CreateObject("WScript.Shell") 
strResult = WshShell.Exec(strCMD).StdOut.ReadAll 
MsgBox strResult 
End Sub

return result wsh script

Print Friendly, PDF & Email

Este artigo foi útil? Was this helpful?

Classificação média - Average rating 0 / 5. Count: 0

Leave a Reply

Your email address will not be published.Required fields are marked *