2013-10-28 500 views
0

我有一個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值回到原來的調用者?我正在嘗試不必將值寫入文件。

回答

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


query-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可以輕鬆使用運行腳本的帳戶的安全上下文。

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