【问题标题】:VBScripts interacting with each otherVBScripts 相互交互
【发布时间】:2013-11-26 08:27:06
【问题描述】:

所以我经常使用 VBScript,并且我有几个调用相同函数的脚本。目前,每个脚本都在底部复制了该功能,但是当我必须进行更新时会很痛苦,因为我必须更新几个文件(而且我通常会忘记一些)。有没有办法可以:
1。让“TestScript1”调用“TestScript2”
2。让“TestScirpt2”从“TestScript1”中获取一个参数(即一个特定的日期变量)
3。让“TestScript2”运行其功能,并将 3 个不同的参数传回“TestScript1”

如果我实际上能够以某种方式完成所有这些工作并让它适用于与“TestScript2”交互的多个脚本。

【问题讨论】:

    标签: vbscript arguments interaction


    【解决方案1】:

    我在一些脚本中使用objShell.Exec 执行此操作。基本上我有一个以我想要执行的函数命名的脚本,我从另一个脚本调用它。

    在“父脚本”中我有一个名为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 
    

    然后在每个“子”脚本中,我接受我通过使用传递给它的参数:
    strComputer = WScript.Arguments(0)
    然后输出我这样写:
    WScript.StdErr.Write "whatever the output is"

    【讨论】:

      【解决方案2】:

      您是否考虑过使用HTA?如果您想加载和组合多个脚本文件,这是一个如何使用 HTA 的示例:

      <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>
      

      testscript2.vbs

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

      【讨论】:

        【解决方案3】:

        我认为最好的方法是创建一个Windows Script Component。这会将您的脚本公开为一个完整的 COM 对象,您可以从任何地方调用 - VBScript 或任何其他支持 COM 的编程语言。

        这是一些示例代码。这是在扩展名为 .wcs 的文件中。

        <?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>
        

        使用以下命令将此文件注册到 COM。

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

        然后您可以使用您在脚本组件中定义的 ProgID 调用 VBScript 中的方法。

        Dim script_functions
        Set script_functions = CreateObject("Tmdean.ScriptFunctions")
        
        WScript.Echo script_functions.MyMethod(2, 2)
        

        【讨论】:

          猜你喜欢
          • 2018-01-31
          • 1970-01-01
          • 2014-02-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-23
          • 2015-03-11
          相关资源
          最近更新 更多