VBScript Higher-Order Functions - vbscript

Is there a way to write anonymous functions, pass them to other functions, in which they are invoked, in vbscript?

There are no anonymous functions/subs/methods in VBScript.
You can use GetRef() (see sample1, sample2) to get something like a function pointer that can be passed to functions/subs to be invoked there (callback). But there are no closures in VBScript, so tricks possible in other languages fail in VBScript.
For specific problems that can be solved with higher order functions in functional languages there may be (nearly) equivalent VBScript solutions involving classes/objects; but for discussing that approach you need to describe your/such a problem in detail.

VBScript has the ability to execute arbitatry code.
Execute and Eval just do what they say to a string containing code.
ExecuteGlobal adds code to your program, like a new function, new variables.
Script Control adds vbscript/jscript scripting language to any program including vbscripts. It can have access to the host's data.
If using ExecuteGlobal/Execute/Eval it is best to run through a scriptcontrol first to test for syntax errors (as you can't trap syntax errors, but you can trap the runtime error the script control gives off on a syntax error).
So you can build your program at runtime.
Set Arg = WScript.Arguments
set WshShell = createObject("Wscript.Shell")
Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout
Sub VBSCmd
RawScript = LCase(Arg(1))
'Remove ^ from quoting command line and replace : with vbcrlf so get line number if error
Script = Replace(RawScript, "^", "")
Script = Replace(Script, "'", chr(34))
Script = Replace(Script, ":", vbcrlf)
'Building the script with predefined statements and the user's code
Script = "Dim gU" & vbcrlf & "Dim gdU" & vbcrlf & "Set gdU = CreateObject(" & chr(34) & "Scripting.Dictionary" & chr(34) & ")" & vbcrlf & "Function UF(L, LC)" & vbcrlf & "Set greU = New RegExp" & vbcrlf & "On Error Resume Next" & vbcrlf & Script & vbcrlf & "End Function" & vbcrlf
'Testing the script for syntax errors
On Error Resume Next
set ScriptControl1 = wscript.createObject("MSScriptControl.ScriptControl",SC)
With ScriptControl1
.Language = "VBScript"
.UseSafeSubset = False
.AllowUI = True
.AddCode Script
End With
With ScriptControl1.Error
If .number <> 0 then
Outp.WriteBlankLines(1)
Outp.WriteLine "User function syntax error"
Outp.WriteLine "=========================="
Outp.WriteBlankLines(1)
Outp.Write NumberScript(Script)
Outp.WriteBlankLines(2)
Outp.WriteLine "Error " & .number & " " & .description
Outp.WriteLine "Line " & .line & " " & "Col " & .column
Exit Sub
End If
End With
ExecuteGlobal(Script)
'Remove the first line as the parameters are the first line
'Line=Inp.readline
Do Until Inp.AtEndOfStream
Line=Inp.readline
LineCount = Inp.Line
temp = UF(Line, LineCount)
If err.number <> 0 then
outp.writeline ""
outp.writeline ""
outp.writeline "User function runtime error"
outp.writeline "==========================="
Outp.WriteBlankLines(1)
Outp.Write NumberScript(Script)
Outp.WriteBlankLines(2)
Outp.WriteLine "Error " & err.number & " " & err.description
Outp.WriteLine "Source " & err.source
Outp.WriteLine "Line number and column not available for runtime errors"
wscript.quit
End If
outp.writeline temp
Loop
End Sub
Vbs
filter vbs "text of a vbs script"
filter vb "text of a vbs script"
Use colons to seperate statements and lines. Use single quotes in place of double quotes, if you need a single quote use chr(39). Escape brackets and ampersand with the ^ character. If you need a caret use chr(136).
The function is called UF (for UserFunction). It has two parameters, L which contains the current line and LC which contains the linecount. Set the results of the script to UF. See example.
There are three global objects available. An undeclared global variable gU to maintain state. Use it as an array if you need more than one variable. A Dictionary object gdU for saving and accessing previous lines. And a RegExp object greU ready for use.
Example
This vbs script inserts the line number and sets the line to the function UF which Filter prints.
filter vbs "uf=LC ^& ' ' ^& L"<"%systemroot%\win.ini"
This is how it looks in memory
Dim gU
Set gdU = CreateObject("Scripting.Dictionary")
Set greU = New RegExp
Function UF(L, LC)
---from command line---
uf=LC & " " & L
---end from command line---
End Function
If there is a syntax error Filter will display debugging details.
User function syntax error
==========================
1 Dim gU
2 Dim gdU
3 Set greU = CreateObject("Scripting.Dictionary")
4 Function UF(L, LC)
5 On Error Resume Next
6 uf=LC dim & " " & L
7 End Function
Error 1025 Expected end of statement
Line 6 Col 6
User function runtime error
===========================
1 Dim gU
2 Dim gdU
3 Set greU = CreateObject("Scripting.Dictionary")
4 Function UF(L, LC)
5 On Error Resume Next
6 uf=LC/0 & " " & L
7 End Function
Error 11 Division by zero
Source Microsoft VBScript runtime error
Line number and column not available for runtime errors

the funny thing about function objects is that they by definition are a memory leak. This means that once you create a function object, you need to keep the scope it was created in intact, which threw me off.
Class VBCompiler
Public leaks
Public Sub Class_Initialize()
leaks = Array()
End Sub
Public Function Compile(code)
Dim compiler, result
Set compiler = CreateObject("MSScriptControl.ScriptControl")
Set portal = CreateObject("Scripting.Dictionary")
Dim name
compiler.Language = "VBScript"
compiler.AddObject "portal", portal, True
compiler.ExecuteStatement code
name = compiler.Procedures(1).Name
compiler.ExecuteStatement "portal.Add ""result"", GetRef(""" & name & """)"
' save the script control because if we go out of scope...
' our function object goes poof!
' leaks.Push compiler
ReDim Preserve leaks(UBound(leaks) + 1)
Set leaks(UBound(leaks)) = compiler
Set Compile = portal("result")
End Function
End Class
Dim z
Set z = New VBCompiler
Set z2 = z.Compile("Function Foo(s):MsgBox s:Foo = 2:End Function")
z2("Hi!")
z2 "Hello Again!"
Gives the two message boxes as desired
Class VBCompiler
Public Function Compile(code)
Dim compiler, result
Set compiler = CreateObject("MSScriptControl.ScriptControl")
Set portal = CreateObject("Scripting.Dictionary")
Dim name
compiler.Language = "VBScript"
compiler.AddObject "portal", portal, True
compiler.ExecuteStatement code
name = compiler.Procedures(1).Name
compiler.ExecuteStatement "portal.Add ""result"", GetRef(""Foo"") "
Set Compile = portal("result")
End Function
End Class
Dim z
Set z = New VBCompiler
Set z2 = z.Compile("Function Foo():MsgBox ""Well Met!"":Foo = 2:End Function")
z2("Hi!")
z2 "Hello Again!"
The above gives (29, 5) (null): Unspecified error. This error is in essence: your object has committed suicide.
This approach can be improved(in particular, the issue of wasteful one ScriptControl per compilation without any plans to release them).

Related

Is there a way to run VbScript in EA from command line?

I'd like to run my VBScript code written in EA from command line (as a night job). Is there a way to do it except copying it to .vbs file and run it like this? I have a bunch of !INC-ludes in the script and that would mean double maintenance effort to keep both versions up-to-date. Is there a solution?
No out-of-the-box solution is available here. But you can write VBScript code to mimic what EA does, when it runs the script, i.e.
retrieve the script code from t_script table
retrieve all icludes (!INC script_group.script_name)
replace the !INC lines with the included script text
put the script into temporary .VBS file and run it
Further, you need to provide replacement for all the features readily available in EA when running a script, at least
create instance of the Repository object as global variable
replace all the Session.Output statements to get output to the console (alternatively, you may implement and instantiate your own Session class)
As EA is a 32-bit application, you need to call 32-bit version of the scripting engine (cscript.exe) to run the script communicating with EA API, usualy at C:\Windows\SysWOW64\cscript.exe
All the tasks above are compiled into the following script code:
option explicit
'(c) Martin Jecny 2020
'WScript.Echo "Scripting Engine: " & ScriptEngine & " ver. " & ScriptEngineMajorVersion & "." & ScriptEngineMinorVersion & "." & ScriptEngineBuildVersion
'WScript.Echo "Creating Repository object"
dim Repository 'as EA.Repository 'defined as global, as it is default in EA environment
set Repository=CreateObject("EA.Repository")
dim ses 'as EA.Session
WScript.Echo "Start: " & Now
runIt(WScript.Arguments) 'complete script name, -v|--verbose
WScript.Echo "End: " & Now
'cleanup
on error resume next
Repository.exit
set Repository=Nothing
on error goto 0
sub runIt(argList)
dim result 'as Variant
if argList.Count <1 then
WScript.Echo "Usage: runEaScript <scriptGroup>.<scriptName> [-v|--verbose] [-c|--copysource]"
result=cleanup(Nothing)
WScript.Quit(0)
end if
'parse arguments
dim scriptFullName 'as String
scriptFullName=argList(0)
dim arg
dim verbose 'as Boolean
verbose=false
dim copysource 'as Bollean
copysource=false
for each arg in argList
select case arg
case "-v", "--verbose"
WScript.Echo "Verbose mode on"
verbose=true
case "-c", "--copysource"
copysource=true
if verbose then
WScript.Echo "Copy of the produced code will be stored to current directory"
end if
end select
next
if verbose then WScript.Echo "Requested script to run: '" & scriptFullName & "'"
if verbose then WScript.Echo "Opening cloud Repository ..."
Repository.OpenFile ("mycloudrepository --- ;Connect=Cloud=protocol:http,address:cloudhost,port:80;Data Source=modelname;DSN=modelname;LazyLoad=1;")
if verbose then WScript.Echo "Retrieving main script code ..."
dim sql 'as String
dim mainScriptCode 'as String
mainScriptCode=getScriptCode(scriptFullName)
if Len(mainScriptCode)<1 then
WScript.Echo "500002" & ": " & "Main script code retrieval failed."
result=cleanup(Nothing)
WScript.Quit(3)
end if
if verbose then WScript.Echo "Resolving !INCludes ..."
dim startPos 'as Integer 'position of !INC in the code
dim endOfPreviousLinePos 'as Integer 'position before start of the !INC line
dim startToInc 'as String ' string between start of line and !INC directive
dim endLinePos 'as Integer 'end position of !INC line
dim endIncPos 'as Integer 'end of included script name within the line
startPos=1 'start position of !INC in script code
endLinePos=0
endIncPos=0
dim includeList 'as Scripting.Dictionary 'list of already included scripts
set includeList=CreateObject("Scripting.Dictionary")
includeList.RemoveAll
dim includeString 'as String '!INC <script> string
dim toBeReplaced 'as String 'usualy full line with !INC string
do while startPos<>0
'detect !INC
startPos = InStr(1,mainScriptCode,"!INC ")
'detection and removal of !INC within commented line
if startPos > 0 then
endLinePos=InStr(startPos,mainScriptCode,chr(10))
endOfPreviousLinePos=InStrRev(mainScriptCode, chr(10),startPos)
if endOfPreviousLinePos <> (startPos-1) then
startToInc=mid(mainScriptCode,endOfPreviousLinePos,startPos-endOfPreviousLinePos)
if InStr(startToInc,"'")>0 then
if verbose then WScript.Echo "Skipping commented reference " & startToInc & toBeReplaced
toBeReplaced=mid(mainScriptCode,startPos,endLinePos-startPos)
mainScriptCode=Replace(mainScriptCode,toBeReplaced,"",1,1)
startPos=InStr(1,mainScriptCode,"!INC ")
end if
end if
end if
'including the code if not already included
if startPos > 0 then
endLinePos=InStr(startPos,mainScriptCode,chr(10))
includeString=trim(mid(mainScriptCode,startPos+5,endLinePos-(startPos+5))) 'ommit !INC string
toBeReplaced=mid(mainScriptCode,startPos,endLinePos-startPos)
'remove comment from reference line
endIncPos=InStr(1,includeString,"'") 'comment?
if endIncPos>0 then 'strip comment after reference
includeString=left(includeString,endIncPos-1)
end if
includeString=trim(includeString)
Err.Clear 'probably not necessary, just for sure
on error resume next
includeList.Add includeString,includeString 'Dictionary object has natively unique index
if Err.Number >0 then 'already exists
if verbose then WScript.Echo includeString & " already included, removing the reference."
mainScriptCode=Replace(mainScriptCode,toBeReplaced,"",1,1)
else 'new one found
if verbose then WScript.Echo "Including '" & includeString & "'"
mainScriptCode=Replace(mainScriptCode,toBeReplaced,getScriptCode(includeString),1,1)
end if
on error goto 0
end if
loop
'adapt code for running in pure VBS environment
if verbose then WScript.Echo "Adapting the code ..."
mainScriptCode=adaptToPureVbsCode(mainScriptCode)
'make file with the code to run
dim tempFileName 'as String
dim fso 'as Scripting.FileSystemObject
dim tempFolder 'as Folder
set fso=CreateObject("Scripting.FileSystemObject")
set tempFolder=fso.GetSpecialFolder(2) 'get temp diectory
tempFileName=fso.GetSpecialFolder(2).Path & "\" & fso.getTempName
dim mainScriptFile 'as File
set mainScriptFile=fso.createTextFile(tempFileName)
result=mainScriptFile.Write(mainScriptCode) '#TODO error handling
result=mainScriptFile.Close
if verbose then WScript.Echo "Written to file: " & tempFileName
if copysource then
dim scriptdir 'as Folder
scriptdir = fso.GetParentFolderName(WScript.ScriptFullName)
result=fso.CopyFile (tempFileName, scriptdir & scripFullName& ".vbs",true) 'overwrite allowed
end if
executeGlobal fso.openTextFile( tempFileName).readAll() 'run the complete script from temporary file
if verbose then Wscript.Echo "000000" & ": " & "Successful exit"
WScript.Quit(0)
end sub
function getScriptCode(scriptFullName)
if Len(scriptFullName)<1 then
WScript.Echo "500001" & ": " & "No script name provided"
getScriptCode=""
exit function
end if
if InStr(scriptFullName,".")<2 then
WScript.Echo "500004" & ": " & "No group - provide full script name in the form <Group>.<Script>"
getScriptCode=""
exit function
end if
dim dotPos 'as Integer
dotPos=InStr(scriptFullName,".")
dim scriptGroupName 'as String
dim ScriptName 'as String
scriptGroupName=Left(scriptFullName,dotPos-1)
scriptName=Mid(scriptFullName,dotPos+1)
if Len(scriptName)<1 then
WScript.Echo "500005" & ": " & "No script name - provide full script name in the form <Group>.<Script>"
getScriptCode=""
end if
dim sql
sql="select s.script from t_script s, t_script g where s.scriptauthor=g.scriptname"
sql = sql & " and g.script like '" & scriptGroupName & "'"
sql = sql & " and s.notes like '%Script Name=""" & scriptName & """%'"
dim scriptCode 'as String
getScriptCode=getSqlSingleValue(sql)
end function
'* adapts the code to run in pure VBS outside of EA
'* #param String 'original EA VBS code
'* #return String 'code with replacements
function adaptToPureVbsCode(code) '#TODO: replacement for Session.Input and Session.Prompt
dim regEx 'as RegExp
set regEx=New RegExp
regEx.IgnoreCase=true
regEx.Global=true
regEx.Pattern=chr(10)
'beautification of the code, mainly for debug purposes
code=regEx.Replace(code,chr(13) & chr(10))
'redirect outuput commands
regEx.Pattern="session.output" 'replace output command
code=regEx.Replace(code,"WScript.Echo")
'comment out manipulation with script output window
regEx.Pattern="Repository.EnsureOutputVisible \""Script\"""
code=regEx.Replace(code,"'"& "Repository.EnsureOutputVisible ""Script""")
regEx.Pattern="Repository.ClearOutput \""Script\"""
code=regEx.Replace(code,"'Repository.ClearOutput ""Script""")
adaptToPureVbsCode=code
end function
'* returns single (or first) value from single column; SQL query must comply to this; returns empty string if not found
'* #param sql as String SQL query
'* #return String
public function getSqlSingleValue(sql) 'as String
dim xmlDoc 'as MSXML2.DomDocument60 '1.2.0
set xmlDoc=CreateObject("Msxml2.DOMDocument.6.0") '1.2.0
dim node 'as MSXML2.IXMLDomNode
xmlDoc.loadXML(Repository.SQLQuery(sql)) '#TODO fails with field names like COUNT(*) on "("; needs escaping
set node= xmlDoc.selectSingleNode("//Row[1]/child::node()")
if node is nothing then
getSqlSingleValue=""
exit function
end if
if len(node.text)>0 then
getSqlSingleValue=node.text
else
getSqlSingleValue=""
end if
end function
'*# Cleanup of the environment
'*#param FileSystemObject fso
'*#return void
function cleanup(fso)
on error resume next
Repository.CloseFile
Repository.Exit
set Repository=nothing
result=fso.DeleteFile(tempFileName)
set fso=nothing
on error goto 0
end function
Script Usage
"C:\Windows\SysWOW64\cscript.exe" "C:\Users\user\Documents\jobs\runEaScript.vbs" "my Script Group.My Script Name" --verbose /nologo
It has to be noted, that VBScript's text operations are not extermely fast and script preparation may take some time. Should you need to run the script very often, implement some caching of the resulting VBS file.

Vbscript Function Print Output

I have create VBscript to enumerate user in group
Function GetUserInGroup()
strComputer = "localhost"
Set colGroups = GetObject("WinNT://" & strComputer & "")
colGroups.Filter = Array("group")
For Each objGroup In colGroups
For Each objUser in objGroup.Members
If objUser.name = "yayantritaryana" Then
WScript.stdout.write objGroup.Name + " "
End If
Next
Next
End Function
WScript.stdout.write "Group=" + GetUserInGroup
But when i execute it, the output is'nt what i expected
The output I Wanted is like
Group=Administrator SQLAdmin Sysadmin
Can someone help me ?
A Function has a purpose (eg. delivering a string of space separated group names). For that you assign the desired result to the function's name (other languages use some kind of return statement):
Function GetUserInGroup()
GetUserInGroup = "pi pa po"
End Function
WScript.stdout.write "Group=" & GetUserInGroup()
output:
cscript 29053176.vbs
Group=pi pa po
A function shouldn't have side-effects (like your
WScript.stdout.write objGroup.Name + " "
which prints names to the console before you output "Group=" in the last line of your script). Instead concatenate the objGroup.Names.
The operator for string concatenation is &.
The (possibly empty) argument list of a function call must be enclosed by (); these param list () are illegal for Sub calls.
Some extra code as food for thought:
Function GetUserInGroup()
For Each s In Split("pi pa po")
GetUserInGroup = GetUserInGroup & s & "*"
' GetUserInGroup = GetUserInGroup() & s & "*"
Next
End Function
WScript.stdout.write "Group=" & GetUserInGroup()

'for Each objDrive in colDrives' creating function syntax error

I am trying to create a .vbs that will check is a dvd drive exists (if objdrive.drivetype= 4) while ignoring other drives such as hard drives (else if cdrive = 1 then -no statement- ect.).
However this line is causing me grief: "For Each objDrive in colDrives". When it exists it causes a syntax error, yet when it is removed it causes an error saying "object required: objdrive". The script uses a hta/vbs hybrid that offers the user to cancel the search for media, and this is achieved by using a function so putting this in a sub and calling this would be useless. Here is my code, please help.
Set shell=CreateObject("wscript.shell")
Set objShell = Wscript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = objFSO.Drives
For Each objDrive in colDrives
if objdrive.drivetype= 4 then
select case 1
case 1
if objdrive.isready then
'continue statement here
else
select case 2
case 2
with HTABox("#F2F2F2", 115, 300, 700, 400)
.document.title = "Waiting..."
.msg.innerHTML = "Waiting for playable media...<b>"
end with
function HTABox(sBgColor, h, w, l, t)
Dim IE, HTA
randomize : nRnd = Int(1000000 * rnd)
sCmd = "mshta.exe ""javascript:{new " _
& "ActiveXObject(""InternetExplorer.Application"")" _
& ".PutProperty('" & nRnd & "',window);" _
& "window.resizeTo(" & w & "," & h & ");" _
& "window.moveTo(" & l & "," & t & ")}"""
with CreateObject("WScript.Shell")
.Run sCmd, 1, False
do until .AppActivate("javascript:{new ") : WSH.sleep 10 : loop
end with ' WSHShell
For Each IE In CreateObject("Shell.Application").windows
If IsObject(IE.GetProperty(nRnd)) Then
set HTABox = IE.GetProperty(nRnd)
IE.Quit
HTABox.document.title = "Waiting"
HTABox.document.write _
"<HTA:Application contextMenu=no border=thin " _
& "minimizebutton=no maximizebutton=no sysmenu=no />" _
& "<body scroll=no style='background-color:" _
& sBgColor & ";font:normal 10pt Arial;" _
& "border-Style:normal;border-Width:0px'" _
& "onbeforeunload='vbscript:if (done.value or cancel.value) then " _
& "window.event.cancelBubble=false:" _
& "window.event.returnValue=false:" _
& "cancel.value=false: done.value=false:end if'>" _
& "<input type=hidden id=done value=false>" _
& "<input type=hidden id=cancel value=false>" _
& "<center><span id=msg> </span><br>" _
& " <center><input type=button id=btn1 value=Cancel
' "_
& "onclick=self.close><center></body>"
exit function
End If
Next
MsgBox "HTA window not found."
wsh.quit
End Function
end select
end select
else if objdrive.drivetype = 1 then
else if objdrive.drivetype = 2 then
else if objdrive.drivetype = 3 then
else if objdrive.drivetype = 5 then
end if
The syntax error is most likely caused by the missing Next keyword that would close the loop. I think the conditional if objdrive.isready then is missing a closing End If too (between the two End Select). Add the missing keywords and the error should go away.
However, you're doing this whole thing upside down. Why are you creating an HTA on the fly from a VBScript? Just write the HTA and embed whatever VBScript code you need in it. See this tutorial for an introduction. Also, I would strongly recommend avoiding nested function definitions. They will cause you maintenance headaches at some point and they're not even generally allowed in VBScript. And what are your Select statements supposed to do? A construct
Select Case 1
Case 1
'instruction
End Select
is utterly pointless, because there is no selection in the first place. It's the exact same as running the instruction directly. Another thing to avoid are empty actions in conditionals. They just make your code harder to read and to maintain without generating you any benefit.
Its possible your issue may be due to the Upper Case D in "objDrive" in your For statement and then you later reference the name with a lower case "d" objdrive.isready within the loop. You may want to declare 'Option Explicit' at the top to find all undeclared variables.
Can you test the below code and see if it performs properly.
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objDrive in objFSO.Drives
If objDrive.DriveType = 4 Then
If objDrive.IsReady Then
MsgBox "The appropriate media is inserted and ready for access"
Else
MsgBox "The Drive Is Not Ready"
End If
End If
Next
Also, I'm not sure the code snippet you provided is your full code but there appear to be several missing End statements. If so, these may also cause you problems.

Executing VBScript file from Excel VBA macros

I need some excel vba examples, where with in the VBA code(Excel Macro) i could call a VBScript and will get some values like filename and directory information from the vbscript and assign it to the variables in VBA code.
Thank you in advance
Some thing like this
VBA macro:
Sub Foo2Script
Dim x As Long
x=2
'Call VBscript here
MsgBox scriptresult
End Sub
VBScript:
Dim x, y ,Z
x = x_from_macro
y = x + 2
Z = X+Y
scriptresult = y,Z
It can be done but I would have to agree with Tomalak and others that it's not the best way to go. However, saying that, VBScript can work wonders occasionally if you use it as a kind of fire and forget mechanism. It can be used quite effectively to simulate multi-threading in VBA whereby you breakdown the payload and farm it out to individual VBScripts to run independently. Eg you could arrange a "swarm" of individual VBScripts to mass download from websites in the background whilst VBA continues with other code.
Below is some VBA code I've simplified to show what can be done and writes a simple VBScript on the fly. Normally I prefer to run it using 'wshShell.Run """" & SFilename & """" which means I can forget about it but I've included in this example this method Set proc = wshShell.exec(strexec) which allows a test of the object for completion
Put this in MODULE1
Option Explicit
Public path As String
Sub writeVBScript()
Dim s As String, SFilename As String
Dim intFileNum As Integer, wshShell As Object, proc As Object
Dim test1 As String
Dim test2 As String
test1 = "VBScriptMsg - Test1 is this variable"
test2 = "VBScriptMsg - Test2 is that variable"
'write VBScript (Writes to Excel Sheet1!A1 & Calls Function Module1.ReturnVBScript)
s = s & "Set objExcel = GetObject( , ""Excel.Application"") " & vbCrLf
s = s & "Set objWorkbook = objExcel.Workbooks(""" & ThisWorkbook.Name & """)" & vbCrLf
s = s & "Set oShell = CreateObject(""WScript.Shell"")" & vbCrLf
s = s & "Msgbox (""" & test1 & """)" & vbCrLf
s = s & "Msgbox (""" & test2 & """)" & vbCrLf
s = s & "Set oFSO = CreateObject(""Scripting.FileSystemObject"")" & vbCrLf
s = s & "oShell.CurrentDirectory = oFSO.GetParentFolderName(Wscript.ScriptFullName)" & vbCrLf
s = s & "objWorkbook.sheets(""Sheet1"").Range(""" & "A1" & """) = oShell.CurrentDirectory" & vbCrLf
s = s & "Set objWMI = objWorkbook.Application.Run(""Module1.ReturnVBScript"", """" & oShell.CurrentDirectory & """") " & vbCrLf
s = s & "msgbox(""VBScriptMsg - "" & oShell.CurrentDirectory)" & vbCrLf
Debug.Print s
' Write VBScript file to disk
SFilename = ActiveWorkbook.path & "\TestVBScript.vbs"
intFileNum = FreeFile
Open SFilename For Output As intFileNum
Print #intFileNum, s
Close intFileNum
DoEvents
' Run VBScript file
Set wshShell = CreateObject("Wscript.Shell")
Set proc = wshShell.exec("cscript " & SFilename & "") ' run VBScript
'could also send some variable
'Set proc = wsh.Exec("cscript VBScript.vbs var1 var2") 'run VBScript passing variables
'Wait for script to end
Do While proc.Status = 0
DoEvents
Loop
MsgBox ("This is in Excel: " & Sheet1.Range("A1"))
MsgBox ("This passed from VBScript: " & path)
'wshShell.Run """" & SFilename & """"
Kill ActiveWorkbook.path & "\TestVBScript.vbs"
End Sub
Public Function ReturnVBScript(strText As String)
path = strText
End Function
This demonstrated several ways that variables can be passed around.

Passing objects as arguments in VBScript

I'm working on a project to capture various disk performance metrics using VBScript and would like to use a sub procedure with an object as an argument. In the following code samples the object I'm referring to is objitem.AvgDiskQueueLength which will provide a value for the disk queue length. I haven't found a way to make it work since it is recognized as a string and then doesn't capture the value. My goal is to make it easy for anyone to change the counters that are to be captured by only having to make a change in one location(the procedure call argument). The way I'm going about this may not be the best but I'm open to suggestions. The sub procedure call is below.
PerfCounter "Average Disk Queue Length", "disk_queueLength", "objItem.AvgDiskQueueLength"
The following code is the sub procedure.
Sub PerfCounter(CounterDescription, CounterLabel, CounterObject)
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfDisk_PhysicalDisk",,48)
args_index = args_index + 1
arrCriteria = split(command_line_args(args_index),",")
strDriveLetter = UCase(arrCriteria(0))
intCriticalThreshold = arrCriteria(1)
intWarningThreshold = arrCriteria(2)
For Each objItem in colItems
With objItem
WScript.Echo "objitem.name = " & objitem.name
If InStr(objItem.Name, strDriveLetter & ":") > 0 Then
intChrLocation = InStr(objItem.Name, strDriveletter)
strInstanceName = Mid(objItem.Name, intChrLocation, 1)
End If
If strDriveLetter = strInstanceName AND InStr(objItem.Name, strDriveLetter & ":") > 0 Then
If intActiveNode = 1 OR Len(intActiveNode) < 1 Then
WScript.Echo "CounterDescription = " & CounterDescription
WScript.Echo "CounterLabel = " & CounterLabel
WScript.Echo "CounterObject = " & CounterObject
If CInt(CounterOjbect) => CInt(intCriticalThreshold) Then
arrStatus(i) = "CRITICAL: " & strDriveLetter & ": " & CounterDescription
arrTrendData(i) = CounterLabel & "=" & CounterObject
intExitCode = 2
arrExitCode(i) = intExitCode
ElseIf CInt(CounterOjbect) => CInt(intWarningThreshold) AND CInt(CounterObject) < CInt(intCriticalThreshold) Then
arrStatus(i) = "WARNING: " & strDriveLetter & ": " & CounterDescription
arrTrendData(i) = CounterLabel & "=" & CounterObject
intExitCode = 1
arrExitCode(i) = intExitCode
Else
arrStatus(i) = "OK: " & strDriveLetter & ": " & CounterDescription
arrTrendData(i) = CounterLabel & "=" & CounterObject
intExitCode = 0
arrExitCode(i) = intExitCode
End If
Else
PassiveNode CounterDescription, CounterLabel
End If
End If
End With
Next
i = i + 1
ReDim Preserve arrStatus(i)
ReDim Preserve arrTrendData(i)
ReDim Preserve arrExitCode(i)
End Sub
Why cant you do this...
PerfCounter "Average Disk Queue Length", "disk_queueLength", objItem.AvgDiskQueueLength
To pass an object you have to pass an object, not a string. To make this method work as expected you would have to have the object prior to the procedure call, but in your code example it looks like you are trying to pass an object that you don't have. A working example:
Set objFSO = CreateObject("Scripting.FileSystemObject")
UseFileSystemObject objFSO
Sub UseFileSystemObject( objfso)
'Now I can use the FileSystemObject in this procedure.
End Sub
But calling the UseFileSystemObject procedure like this will not work,
UseFileSystemObject "objFSO"
because you are passing in a string not an object.
The only way I can think of to accomplish what you want is to use a select statement to write the appropriate attribute of the object, something like this.
Call PerfCounter "Average Disk Queue Length", "disk_queueLength", "AvgDiskQueueLength"
Sub PerfCounter(CounterDescription, CounterLabel, CounterObjectAttribute)
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfDisk_PhysicalDisk",,48)
For Each objItem in colItems
Select Case CounterObjectAttribute
Case "ObjectAttribute1"
Case "ObjectAttribute2"
Case "AvgDiskQueueLength"
Wscript.Echo objItem.AvgDiskQueueLength
End Select
Next
End Sub
So in the select you would have to add a case for each attribute that can be used, but it would allow you to pass a string into the procedure. I might be way off on this, but I don't know how you can pass an object if you don't have the object first.

Resources