Solved

Calling An External VBScript Function/Routine With Return Parameters

Posted on 2009-07-05
9
1,586 Views
Last Modified: 2012-05-07
I want to run a VBScript function from another VBScript with parameters.  How is this done?

'C:\Script1.vbs
Dim MyVar
MyVar = Blah(1, 2, 3)

'C:\Scrip2.vbs
Function Blah(Num1, Num2, Num3)
  Blah = Num1 + Num2 + Num3
End Function


Mike
0
Comment
Question by:shacho
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 5

Assisted Solution

by:Tompa99
Tompa99 earned 200 total points
ID: 24782828
Hi there

Using one main script Main.wsf like the one in the Code Snippet in this I do include one vbs script
Function.vbs with the following code:

private function GetComputerName()

      Dim objWshNetwork
      Set objWshNetwork = WScript.CreateObject("WScript.Network.1")
      GetComputerName = objWshNetwork.ComputerName
      
end function

Best Regards Tompa


<job id="[NameOfJob]">
	<!-- Add more lines or remove line below depending on how many subscripts you need-->
	<script language="VBScript" src="Functions.vbs">
 
Call Main
 
'************************** Main () ******************
Sub Main
Dim strComputerName
		strComputerName = GetComputerName()
		MsgBox strComputerName
End Sub
 
</script>
</job>

Open in new window

0
 
LVL 65

Accepted Solution

by:
RobSampson earned 300 total points
ID: 24783092
Hi, you can also try using the ExecuteGlobal method inside Script1.vbs to read in the code from Script2.vbs and then execute it's function.  Something like the below may work.

Regards,

Rob.
' First read in the Script2.vbs contents and use ExecuteGlobal to include it's code
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
strFunctionsFile = "Script2.vbs"
Set objFunctions = objFSO.OpenTextFile(strFunctionsFile, intForReading, False)
strFunctionsCode = objFunctions.ReadAll
objFunctions.Close
ExecuteGlobal strFunctionsCode
 
'Now call a function
strReturnCode = Blah(1, 2, 3)
MsgBox "Return code from included function is " & strReturnCode

Open in new window

0
 

Author Comment

by:shacho
ID: 24783134

Tompa - I don't see how you are specifying the path to the second script.
Rob - Interesting approach.

Mike
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 65

Expert Comment

by:RobSampson
ID: 24783154
Tompa's reference to the second script is this bit:
src="Functions.vbs"

The file is a WSF file, as opposed to a VBS file, so you can save it as WSF and try it.

You can also try my approach, let me know if it works out.

Regards,

Rob.
0
 

Author Comment

by:shacho
ID: 24783530
Hmm.  I suppose that would work.  But is there really no way to call functions script to script (vbs only) with return values?
0
 
LVL 5

Expert Comment

by:Tompa99
ID: 24783859
Hi,

Not that I know. WSF or like Rob suggested are 2 solutions.

Best Regards Tompa
0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 300 total points
ID: 24790646
No, there is no other way.  They do not work like include files on a web page.  I guess one more option, although a bit harder to use, is to use objShell.Run in script1.vbs, to run script2.vbs, while passing some arguments, then in script2.vbs, you read those command line arguments with
wscript.arguments(#)

Then, script2.vbs can return one numeric value with
wscript.quit(intNumber)

which is caught by the objShell.Run method of script1.vbs.  If you want to return text though, you'll have to have script2.vbs write to a file, then Script1.vbs will read that file once Script2.vbs is complete.

Regards,

Rob.
0
 

Author Comment

by:shacho
ID: 24902384
Rob - that's a pretty clever workaround.  Thanks all for your comments.

Mike
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 24910317
Great, thanks for the grade,

Regards,

Rob.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question