如何将参数从批处理(.bat)传递到VBScript(.vbs)?

时间:2012-06-29 15:49:56

标签: parameters vbscript batch-file

如何将参数从批处理传递给vbscript?我的批处理脚本在自动执行结束时发送一封电子邮件。为此,它使用/调用我的vbscript(email.vbs),它发送带有实际日志文件(具有执行结果)的电子邮件。 所有这些日志文件都存储在特定文件夹中,例如:201207(2012年7月),201208(2012年8月)等等.... 我想传递文件夹名称或部分(我正在考虑对2012年部分进行硬编码,并通过我的批次从该参数获取月份数))作为我的email.vbs的参数,以便它可以寻找正确的抓取正确的日志文件的文件夹。有意义吗?

ECHO Checking the log file for errors...
FINDSTR /C:"RC (return code) = 0" %workDir%\%filenm%_Log.txt && (ECHO Deployment was successful. 
ECHO Email is sent out...
cscript //nologo success_mail_DEV.vbs %workDir%  'passing the directory param. here.
ECHO Press ENTER to exit...
GOTO offshore) || (ECHO Deployment was not successful. Errors were found!
ECHO Email is sent out...
ECHO Press ENTER to exit...
cscript //nologo fail_mail_DEV.vbs %workDir%  'and here
GOTO offshore)

这是我现在拥有的一部分。它正在检查日志文件中的错误并相应地调用该成功/失败邮件。现在,您在那里看到的2个vbs邮件脚本中硬编码了位置名称/编号。我确信有一种方法可以将某个参数传递给电子邮件vbscript。但是,我不知道该怎么做。

这是我的邮件vbscript:

Const ForReading = 1

Set args = WScript.Arguments
directory = args.Item(0) 'thought that workDir would come in here


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(&directory&"\filename.txt", ForReading)
fileName = objTextFile.ReadLine

Dim ToAddress
Dim FromAddress
Dim MessageSubject
Dim MyTime
Dim MessageBody
Dim MessageAttachment
Dim ol, ns, newMail
MyTime = Now

ToAddress = "destination@email.com"
MessageSubject = "SUCCESS"
MessageBody = "It was successful" 
MessageAttachment = &directory&"\"&fileName&"_Log.txt"
Set ol = WScript.CreateObject("Outlook.Application")
Set ns = ol.getNamespace("MAPI")
Set newMail = ol.CreateItem(olMailItem)
newMail.Subject = MessageSubject
newMail.Body = MessageBody & vbCrLf & MyTime
newMail.RecipIents.Add(ToAddress)
newMail.Attachments.Add(MessageAttachment)
newMail.Send

objTextFile.Close

但不工作......

提前致谢!

3 个答案:

答案 0 :(得分:5)

回答这个问题......

以这种方式调用你的脚本:

cscript //nologo success_mail_DEV.vbs  ARG1  ARG2

通过WScript.Arguments处理vbscript中的参数。


但我可以建议您完全消除系统的批处理部分。

VBSCript完全能够调用FINDSTR并处理输出。或者实际上你可以完全在VBScript中实现搜索,而根本不需要调用FINDSTR。

答案 1 :(得分:3)

好的,我正在更新我的问题。伙计们,在你们所有的帮助下,它终于工作了。 谢谢。 以下是我调用vbscript并将参数传递给它的方法:

cscript //nologo fail_mail.vbs something.sql 'something.sql is the param. that i'm passing.

以下是我的邮件vbscript:

Const ForReading = 1

Set args = WScript.Arguments
arg1 = args.Item(0)  'the parameter from batch comes in here to arg1
...
...
ToAddress = "my@address.com"
MessageSubject = "WORKED"
MessageBody = "Success" 
MessageAttachment = ""&arg1&""  'here those quotes are important. dk why.
Set ol = WScript.CreateObject("Outlook.Application")
Set ns = ol.getNamespace("MAPI")
Set newMail = ol.CreateItem(olMailItem)
newMail.Subject = MessageSubject
newMail.Body = MessageBody & vbCrLf & MyTime
newMail.RecipIents.Add(ToAddress)
newMail.Attachments.Add(MessageAttachment)
newMail.Send

它正在发挥作用。  *可以使用相同的技术传递多个参数。

答案 2 :(得分:0)

您的VBScript需要一些代码来接受参数,例如:

set args = WScript.Arguments
' Parse args
select case args.Count
case 0
    help
case 1
    sVariable = args(0)
end select

当您调用VBScript时,只需将arg传递给脚本,就像命令一样:

cscript //nologo MyScript.vbs arg