문제

So I use VBScript frequently, and I have several scripts that call the same function. At the moment, each script has that function just copied in there at the bottom, but it's a pain when I have to make an updated, because I have to update several files (and I usually forget about some). Is there a way where I can:
1. Have "TestScript1" call "TestScript2"
2. Have "TestScirpt2" take an argument from "TestScript1" (i.e. a specific date variable)
3. Have "TestScript2" run its functions, and pass 3 different arguments back to "TestScript1"

And a bonus would be if I could actually somehow do all of that and have it work for multiple scripts interacting with "TestScript2".

도움이 되었습니까?

해결책

I do this with objShell.Exec in some of my scripts. Essentially I have a script named after the function I want to do and I call it from another script.

In the "parent script" I have a function called runExternal:

Function runExternal(strScript,strComputer)
'strScript is the name of the script/function I'm calling
Set objExec = objShell.Exec("cmd.exe /c cscript.exe """ & strPath & strScript & ".vbs"" " & strComputer)
intDelay = Timer+5
intTimer = Timer 
While objExec.Status = 0 And intTimer <= intDelay
    intTimer = Timer 
Wend 

If objExec.Status = 1 Then 
    strReturn = objExec.StdErr.ReadAll
    writeLog strScript & " returned " & strReturn
Else
    objExec.Terminate 'terminate script if it times out
    writeLog strScript & " timed/errored out and was terminated."
End If
End function 

Then in each "child" script, I accept the argument I passed to it by using:
strComputer = WScript.Arguments(0)
then to output I write this way:
WScript.StdErr.Write "whatever the output is"

다른 팁

Have you considered using an HTA? This is an example how you can use HTA's if you want to load and combine multiple script files:

<html>
<head>
<title>Demo IT</title>

<HTA:APPLICATION
     ID="objShowMe"
     APPLICATIONNAME="HTAShowMe"
     SCROLL="yes"
     SINGLEINSTANCE="yes"
     WINDOWSTATE="maximize"
>

<SCRIPT Language="VBScript" src="testscript2.vbs"/>
<SCRIPT Language="VBScript">

Sub TakeOff
     text = "1 2 3"
     argArray = GiveMeThree(text)
     msgbox argArray(0)
     msgbox argArray(1)
     msgbox argArray(2)
End Sub

</SCRIPT>
</head>

<body>

<h1>In the body</h1>
<input type="button" value="Click me!" onclick="TakeOff">

</body>
</html>

In testscript2.vbs

Public Function GiveMeThree(x)
    GiveMeThree = split(x, " ")
End Function

I think the best way to do this is to create a Windows Script Component. This will expose your script as a fully baked COM object that you can call from anywhere - VBScript or any other programming language that supports COM.

Here's some example code. This is in a file with a .wcs extension.

<?XML version="1.0"?>
<?component error="false" debug="false"?>
<component id="SVInfo">
    <registration
        progid="Tmdean.ScriptFunctions"
        description="Description of your COM object"
        version="1.5"
        clsid="{3267711E-8359-4BD1-84A6-xxxxxxxxxxxx}"/>
        <!-- generate your own GUID to use in the line above -->
    <public>
        <method name="MyMethod"/>
    </public>
    <script language="VBScript">
        <![CDATA[
        Function MyMethod(param1, param2)
            MyMethod = param1 + param2
        End Function
        ]]>
    </script>
</component>

Register this file to COM with the following command.

regsvr32 scrobj.dll /n /i:file://J:\scripts\scriptfunctions.wcs

Then you can call the methods in VBScript using the ProgID that you defined in the script component.

Dim script_functions
Set script_functions = CreateObject("Tmdean.ScriptFunctions")

WScript.Echo script_functions.MyMethod(2, 2)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top