Purpose You want to call up a VBScript function from JavaScript
We generally suggest you use JavaScript or VB.NET exits in GuiXT
instead of VBScript. See JavaScript,
VBScript or VB.NET? for a comparison of the three
options. However, for some tasks VBScript does make sense,
since one can find plenty of VBScript samples in the web.
Solution In our Javacript function we create an
"MSScriptControl" object and execute the VBScript code via this
scripting host.
You may use the following JavaScript functions for this purpose:
JavaScript framework
JavaScript
Example 1 We execute a single
VBScript statement:
/
GuiXT
/ determine name of month
CallJS monthname = vbsExecute "MonthName(8)"
// display the result (test)
Message "&V[monthname]"
Example 2 We read VBScript code from
a .vbs file, add the code in the scripting host and execute a VBScript
function.
Please observe that adding the code should be done only
once; this logic is not implemented in the InputScript below but in
Example 3.
VBScript "monthnames.vbs"
Function MyMonthName(index,
language) SetLocale(language)
MyMonthName = MonthName(index)
End Function
InputScript
GuiXT
// Read VBScript code from .vbs file
CopyText fromFile="monthnames.vbs" toText="code"
// to scripting host
CallJS vbsAddCode "code"
// call up the VBScript function
CallJS monthname = vbsExecute "MyMonthName(8, ""IT"")"
// display the result (test)
Message "&V[monthname]"
Example 3 The user chooses a
computer from a drop-down list and then obtains a list of all processes
running on this computer:
Click on the image for a larger display
We also offer an "Excel" button which downloads the data to a CSV
file, and a filter button which shows all processes with a CPU activity
since the last display.
VBScript "processes.vbs"
' Use WMI (Windows Management Instrumentation) service
' to list the processes on a computer in the local network
Function GetProcesses(computername)
Dim objWMIService, objProcess, colProcess
On Error Resume Next
Set objWMIService = _
GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
& computername & "\root\cimv2")
If objWMIService Is Nothing Then
GetProcesses = ""
Exit function
End if
Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process")
Dim s
s = ""
For Each objProcess In colProcess
With objProcess
If s <> "" Then
s = s & vbCrLf
End If
s = s & csvString(.ProcessId) & ";"
s = s & csvString(LCase(.Name)) & ";"
s = s & csvString(.Name) & ";"
s = s & csvString(GetVBDate(.CreationDate)) & ";"
s = s & csvString(FormatNumber
_
(.WorkingSetSize/1024,0) & " K") & ";"
s = s & csvString(.Priority) & ";"
s = s & csvString(FormatNumber
_
(.UserModeTime/10/1000,0) & " ms") & ";"
s = s & csvString(FormatNumber
_
(.KernelModeTime/10/1000,0) & " ms") & ";"
s = s & csvString(FormatNumber
_
(.ReadOperationCount,0)) & ";"
s = s & csvString(FormatNumber
_
(.WriteOperationCount,0)) & ";"
s = s & csvString(FormatNumber
_
(.WriteTransferCount/1024,0) & " K") & ";"
s = s & csvString(FormatNumber(.HandleCount,0)) & ";"
s = s & csvString(" " & .CommandLine)
End With
Next
GetProcesses = s
End Function
' Date formatting
Function GetVBDate(wd)
GetVBDate = DateSerial(left(wd,4),mid(wd,5,2),mid(wd,7,2)) _
+ TimeSerial(mid(wd,9,2),mid(wd,11,2),mid(wd,13,2))
End Function
' CSV string format, doubling inner "
Function csvString(s)
If s = "" Or VarType(s) = vbNull Then
csvString = ""
Else
csvString = """" & Replace(CStr(s), """", """""") & """"
End If
// computer selected?
if not V[computername]
Return "W: Please select a computer" -statusline
endif
// install VBScript code
if not V[proc_code_added]
// indicate: VBS code loaded
Set V[proc_code_added] "X"
// Read VBScript code from a .vbs file
CopyText fromFile="C:\temp\processes.vbs" toText="code"
// to scripting host
CallJS vbsAddCode "code"
endif
// call up the VBScript function
CallJS vbsExecute "GetProcesses(""&V[computername]"")" "csvproc"
// to table
CopyText toTable="V[proc]" fromText="csvproc" delimiter=";"
// sort by name (ignore case)
Sort table="V[proc]" orderBy="NameLower,ProcessId"
// reset table display parameters
Clear V[proc.stat.*]
Return
InputScript "excel_processes.txt"
GuiXT
// Excel download (CSV table)
// build temporary filename using TEMP
// environment variable and current time
Set V[tempfile] "&%[TEMP]\guixttemp.&V[today_ymdhms].processes.csv"
CopyText fromTable=V[proc] toText="tempexcel" delimiter=";"
CopyText fromText="tempexcel" toFile="&V[tempfile]"
Start "&[tempfile]"
Return
InputScript "active_processes.txt"
GuiXT
// computer selected?
if not V[computername]
Return "W: Please select a computer" -statusline
endif
// save previous results
CopyText fromText="csvproc" toText="csvprocbase"
// call up the VBScript function
CallJS vbsExecute "GetProcesses(""&V[computername]"")" "csvproc"
// create temporary tables of same structure
CreateTable V[proc1] include=V[proc]
CreateTable V[proc2] include=V[proc]
CreateStructure V[procrow1] include=V[proc1]
CreateStructure V[procrow2] include=V[proc2]
// to table
CopyText toTable="V[proc1]" fromText="csvprocbase" delimiter=";"
CopyText toTable="V[proc2]" fromText="csvproc" delimiter=";"
// fill table proc with active processes
Clear V[proc]
Set V[k] 0
label compare
if V[k<&V[proc2.rowcount]]
Set V[k] &V[k] + 1
ReadRow V[procrow2] table=V[proc2] index=&V[k]
ReadRow V[procrow1] table=V[proc1] key="&V[procrow2.ProcessId]"
// new process, or CPU changed?
if not Q[ok] or _
not V[procrow1.UserModeTime=&V[procrow2.UserModeTime]] or _
not V[procrow1.KernelModeTime=&V[procrow2.KernelModeTime]]
AppendRow V[procrow2] table=V[proc]
endif
goto compare
endif
// sort by name (ignore case)
Sort table="V[proc]" orderBy="NameLower,ProcessId"
// reset table display parameters
Clear V[proc.stat.*]
Return