0

我有一个调用第二个 vbscript 并运行一个函数的 vbscript。第二个函数返回一个值。但我不知道如何获得这个值,因为第一个函数的结果返回状态码。

原始调用:fileCASRING(12345678)

脚本 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 值返回给原始调用者?我试图不必将值写入文件。

4

2 回答 2

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

查看对象的文档WshScriptExec


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

请参阅ADODBCommandParameter对象的文档。不要从字符串构建 SQL。

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

于 2013-10-28T18:29:20.647 回答
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
于 2013-10-29T12:43:20.163 回答