### Windows Script Host running

To run a Windows Script Host (WSH) file (extension .vbs), make use of the jShell Library.
Several examples listed below, like HelloWorld, Ping Host.
Example showing HelloWorld
1. Create the Windows Script Host file (wsh-hello.vbs) and store in the DirApp (Objects) folder:
WScript.Echo "Hello world"
WScript.Quit
2. Create a sub to run the WSH script
`Sub runWSH
Private shl As Shell
Dim params As List: params.Initialize
params.Add("/c")
params.Add("start")
params.Add("wsh-hello.vbs")
shl.Initialize("js", "cmd.exe", params)
shl.WorkingDirectory = File.DirApp
shl.Run(-1)
End Sub`
3. Create a sub to handle process completion
`Sub shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
If Success AND ExitCode = 0 Then
Log("Shell process completed with successfully")
Log(StdOut)
Else
Log("Shell process completed with Error: " & StdErr)
End If
ExitApplication
End Sub`
4. Run the sub "runWSH"
As a result the HelloWorld MessageBox is shown.
Example showing HelloWorld as Sub
This is a more advanced sub using stringbuilder to build the scripting code.
The sub mnuWSHHelloWorld_Action is assigned to a menuitem mnuWSHHelloWorld.
The sub process completed is used as described in the HelloWorld example under 3.
`Sub mnuWSHHelloWorld_Action
Dim shl As Shell
Dim lstParams As List: lstParams.Initialize
Dim strVBSCode As StringBuilder
Dim twWSH As TextWriter
Dim strWSHFile As String = "helloworld.vbs"
Try
' Delete the vbs file
File.Delete(File.DirApp, strWSHFile)
' Build the vbs code
strVBSCode.Initialize
strVBSCode.Append("' B4JHowToShell Windows Script Host Test File" & CRLF)
strVBSCode.Append("' Created " & DateTime.Date(DateTime.now) & " " & DateTime.Time(DateTime.now) & CRLF)
strVBSCode.Append("WScript.Echo " & Chr(34) & "Hello World" & Chr(34) & CRLF)
strVBSCode.Append("WScript.Quit" & CRLF)
writeOutput(True, strVBSCode.ToString & CRLF)
writeOutput(False, "Saving file " & strWSHFile & " and running script...")
' Write the code to the file via textwriter
twWSH.Initialize(File.OpenOutput(File.DirApp, strWSHFile, False))
twWSH.Write(strVBSCode.ToString)
twWSH.Flush
twWSH.Close
' Check if vbs file exists and then run the script via shell methods
If File.Exists(File.DirApp, strWSHFile) Then
lstParams.Add("/c")
lstParams.Add("start")
lstParams.Add(strWSHFile)
shl.Initialize("shl", "cmd.exe", lstParams)
shl.WorkingDirectory = File.DirApp
shl.Run(-1)
End If
Catch
Log(LastException.Message)
End Try
End Sub`
Ping a Host
Ping the host www.mysite by loading the script file ping.vbs.
The sub mnuWSHPing_Action is assigned to a menuitem mnuWSHPing.
The sub process completed is used as described in the HelloWorld example under 3.
`Sub mnuWSHPing_Action
Dim shl As Shell
Dim lstParams As List: lstParams.Initialize
Dim strWSHFile As String = "Ping.vbs"
Try
' Check if vbs file exists and then run the script via shell methods
If File.Exists(File.DirApp, strWSHFile) Then
lstParams.Add("/c")
lstParams.Add("start")
lstParams.Add(strWSHFile)
shl.Initialize("shl", "cmd.exe", lstParams)
shl.WorkingDirectory = File.DirApp
shl.Run(-1)
End If
Catch
Log(LastException.Message)
End Sub`
The script file:
Function Ping(strHost)
Dim oPing, oRetStatus, bReturn
Set oPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select * from Win32_PingStatus where address='" & strHost & "'")
For Each oRetStatus In oPing
If IsNull(oRetStatus.StatusCode) Or oRetStatus.StatusCode <> 0 Then
MsgBox(strHost & vbCRLF & "Error: Host is NOT reachable." & vbCRLF & "Status code: " & oRetStatus.StatusCode)
bReturn = False
Else
MsgBox(strHost & vbCRLF & _
"OK: Host is reachable." & vbCRLF & _
"Status code: " & oRetStatus.StatusCode & vbCRLF & _
"Bytes: " & vbTab & oRetStatus.BufferSize & vbCRLF & _
"Time (ms): " & vbTab & oRetStatus.ResponseTime & vbCRLF & _
"TTL (s): " & vbTab & oRetStatus.ResponseTimeToLive)
bReturn = True
End If
Set oRetStatus = Nothing
Next
Set oPing = Nothing
Ping = bReturn
End Function
Ping("www.mysite")

Changed: 2018-06-07 09:23, Category: Shell, File: T6C248M1797.htm, ID: 1797