2014-09-30 12 views
0

我有一個名爲「bootstrap.bat」和一個批處理文件,它像這個 -在啓動時運行批處理文件VB腳本

@echo OFF 
@echo There is an error. 

wscript errorMail.vbs 

@echo Error Message sent 

正如你可以看到它調用一個VB腳本這是剛剛發送一封電郵。 VB腳本是 -

Set objEmail = CreateObject("CDO.Message") 
objEmail.From = "[email protected]" 
objEmail.To = "[email protected]" 
objEmail.Subject = "Error: Generation of Final report failed" 
objEmail.Textbody = "There was an error in generating the Final report for the Test." 
objEmail.Configuration.Fields.Item _ 
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
objEmail.Configuration.Fields.Item _ 
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _ 
    "mailhost.som.net" 
objEmail.Configuration.Fields.Item _ 
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
objEmail.Configuration.Fields.Update 
objEmail.Send 

當我從CMD手動運行「bootstrap.bat」,則得到正確執行VB腳本和郵件發送得到。但我有一個設置,它將在啓動時運行'bootstrap.bat',但當時它不執行VB腳本。我不確定是否需要更改其他任何東西來執行VB腳本。

+2

當批處理文件從啓動時運行時,可能(未測試),默認文件夾不是存儲批處理文件的文件夾,因此找不到vbscript文件。嘗試使用'wscript'%〜dp0errorMail.vbs'(或者腳本的完整路徑,如果它位於其他文件夾中) – 2014-09-30 11:03:51

+0

您也應該使用cscript而不是wscript。不同之處在於wscript會在調用「echo」的地方打開一個消息框,其中cscript寫入控制檯。 – user743414 2014-09-30 11:09:37

+0

MC ND,謝謝你,工作。你能否把你的答案放在這裏,以便我能接受它。 – 2014-09-30 13:22:37

回答

0

您可以嘗試使用一個文件),而不是兩個(保存爲.bat.cmd):

echo off 
:sub echo(off):exit sub 
set off=""'&set off=&cscript /nologo /e:vbscript "%~f0" %*&exit /b %errorlevel% 
:end sub 

Set objEmail = CreateObject("CDO.Message") 
objEmail.From = "[email protected]" 
objEmail.To = "[email protected]" 
objEmail.Subject = "Error: Generation of Final report failed" 
objEmail.Textbody = "There was an error in generating the Final report for the Test." 
objEmail.Configuration.Fields.Item _ 
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
objEmail.Configuration.Fields.Item _ 
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _ 
    "mailhost.som.net" 
objEmail.Configuration.Fields.Item _ 
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
objEmail.Configuration.Fields.Update 
objEmail.Send 

WScript.Echo "Error Message sent" 

還有一個similar approach

相關問題