vbscript +函数调用函数 - 返回值

时间:2013-10-28 17:49:47

标签: vbscript

我有一个调用第二个vbscript的vbscript并运行一个函数。第二个函数返回一个值。但我无法弄清楚如何获取此值,因为第一个函数的结果返回状态代码。

原始电话:fileCASTRING(12345678)

vbscript 1

function fileCASTRING(varRAW)
lresult = CreateObject("WScript.Shell").Run ("c:\windows\syswow64\cscript.exe C:\ERMXData\Config\query-castring.vbs " & varRAW,0,true)
fileCASTRING=1
end function

查询castring.vbs

doctype=WScript.Arguments.Item(0)
Dim strCon
strCon = "DSN=*****; " & _
         "uid=*****;pwd=*****;"
Dim oCon: Set oCon = WScript.CreateObject("ADODB.Connection")
Dim oRs: Set oRs = WScript.CreateObject("ADODB.Recordset")
oCon.Open strCon
Set oRs = oCon.Execute("select ESBLINK_ADMR_CODE from ESBLINK where ESBLINK_DTYP_CODE like '%" + doctype + "%'"")
queryB=oRs.Fields(0).Value
oCon.Close
Set oRs = Nothing
Set oCon = Nothing

我必须这样做,因为运行vbscript 1的程序以64位模式运行,而query-castring.vbs中的代码需要以32位模式运行才能运行。如何将queryB值返回给原始调用者?我试图不必将值写入文件。

2 个答案:

答案 0 :(得分:1)

在两个命令行进程之间进行通信的唯一简便方法是通过StdOut

(请注意,代码未经过测试,但应该让您朝着正确的方向前进。)


VBScript 1

Option Explicit

' ...

Function fileCASTRING(varRAW)
    Dim program, script, cmdline, output

    program = "c:\windows\syswow64\cscript.exe /nologo"
    script = "C:\ERMXData\Config\query-castring.vbs"
    cmdLine = program & " " script & " """ & varRAW & """"
    output = ""

    With CreateObject("WScript.Shell").Exec(cmdLine)
        While Not .StdOut.AtEndOfStream
            output = output & .StdOut.ReadAll
        Wend
    End With

    fileCASTRING = output
End Function

请参阅documentation of the WshScriptExec object


查询castring.vbs

Option Explicit

Dim doctype: doctype = WScript.Arguments.Item(0)
Dim strCon: strCon = "DSN=*****;uid=*****;pwd=*****;"
Dim strSql: "select ESBLINK_ADMR_CODE from ESBLINK where ESBLINK_DTYP_CODE like '%' + ? + '%'"

Dim oCon: Set oCon = WScript.CreateObject("ADODB.Connection")
Dim oCmd: Set oCmd = WScript.CreateObject("ADODB.Command")

oCon.Open strCon

With WScript.CreateObject("ADODB.Command")
    Set .ActiveConnection = oCon
    .CommandText = strSql
    .Parameters.Add(.CreateParameter)
    .Parameters(0).Value = doctype
    With .Execute
        If Not .EOF Then
            WScript.Echo .Fields("ESBLINK_ADMR_CODE").Value
        End If
    End With
End With

oCon.Close

请参阅ADODB CommandParameter objects的文档。不要从字符串构建SQL。

另外,查看“集成安全性”连接字符串 - 不要将纯文本密码存储在代码文件中。如果您告诉它,ADODB可以轻松使用运行该脚本的帐户的安全上下文。

答案 1 :(得分:0)

VBScript 1

  Function getADMRCODE(varRAW)
        Dim program, script, cmdline, output
        program = "c:\windows\syswow64\cscript.exe /nologo"
        script = "C:\ERMXData\Config\common_app\queries\admrcode.vbs"
        cmdLine = program & " " & script & " """ & varRAW & """"
        output = ""

        With CreateObject("WScript.Shell").Exec(cmdLine)
            While Not .StdOut.AtEndOfStream
                output = output & .StdOut.ReadAll
            Wend
        End With
        getADMRCODE = output
    End Function

查询castring.vbs

Dim doctype: doctype = WScript.Arguments.Item(0)
Dim strCon
strCon = "DSN=*****; " & _
         "uid=*****;pwd=*****;"
Dim oCon: Set oCon = WScript.CreateObject("ADODB.Connection")
Dim oRs: Set oRs = WScript.CreateObject("ADODB.Recordset")
oCon.Open strCon
Set oRs = oCon.Execute("select ESBLINK_ADMR_CODE from ESBLINK where ESBLINK_DTYP_CODE LIKE '%" + doctype + "%'")
 WScript.Echo oRs.Fields(0).Value
oCon.Close
Set oRs = Nothing
Set oCon = Nothing