2009-04-21 93 views
14

我救了這個VBScript腳本,以我的本地機器爲c:\ test.vbs:爲什麼這個VBScript會給我一個錯誤?

WScript.StdOut.WriteLine "This is a test" 

當我在命令行中運行它,我得到這個錯誤:

--------------------------- 
Windows Script Host 
--------------------------- 
Script: C:\test.vbs 
Line: 1 
Char: 1 
Error: The handle is invalid. 
Code: 80070006 
Source:  (null) 

--------------------------- 
OK 
--------------------------- 

我得到這個在Windows Vista(SP1)和Windows XP Pro(SP3)下。

回答

6

正如文章中公認的答案所描述的,我的劇本工作時,我從這樣的命令提示符下稱它:

cscript test.vbs 

您還可以更改默認腳本宿主,以便調用CSCRIPT沒有必要每一次。完成之後,原始命令可以不加修改地工作。

cscript //h:cscript //s 

您可以恢復原來的行爲:

cscript //h:wscript //s 

謝謝!

1

我在bug「cscript - print output on same line on console?」中提交了這個解決方案,我覺得這個問題與這個問題有關。

我在我的JavaScript中使用以下「日誌」功能來支持wscript或cscript環境。正如你所看到的,只有在可以的情況下,這個函數纔會寫入標準輸出。

var ExampleApp = { 
    // Log output to console if available. 
    //  NOTE: Script file has to be executed using "cscript.exe" for this to work. 
    log: function (text) { 
     try { 
      // Test if stdout is working. 
      WScript.stdout.WriteLine(text); 
      // stdout is working, reset this function to always output to stdout. 
      this.log = function (text) { WScript.stdout.WriteLine(text); }; 
     } catch (er) { 
      // stdout is not working, reset this function to do nothing. 
      this.log = function() { }; 
     } 
    }, 
    Main: function() { 
     this.log("Hello world."); 
     this.log("Life is good."); 
    } 
}; 

ExampleApp.Main(); 
+0

根據這個http://stackoverflow.com/questions/4999364/try-catch-end-try-in-vbscript vbscript沒有嘗試抓住。你能夠設置一個變量,取決於它是哪種情況下,在VBScript? – barlop 2013-10-26 23:32:40

相關問題