如何将参数从 Batch (.bat) 传递到 VBScript (.vbs)? [英] How to pass a parameter from Batch (.bat) to VBScript (.vbs)?

查看:35
本文介绍了如何将参数从 Batch (.bat) 传递到 VBScript (.vbs)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I pass a parameter from batch to vbscript? My batch script sends out an email at the end of the automated execution. For that it uses/calls my vbscript (email.vbs) that sends out the email with an actual log file (that has the results for that execution) attached. All those log files are stored in specific folders such as: 201207(July, 2012), 201208(August, 2012) and so on.... I want to pass the folder name or part of (i am thinking about hard coding the 2012 part and get the month number from that parameter through my batch) it as a parameter to my email.vbs so that it can go look for the right folder to grab the right log file. Makes sense?

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.但是,我不知道该怎么做.

This is part of what I have right now. It is checking for errors in the log file and calling that success/failed mail accordingly. Right now the location name/number is hardcoded in those 2 vbs mailing scripts that you see up there. I am sure there is a way to pass a parameter somewhere up there to the emailing vbscript. But, i don't know how to.

这是我的邮寄 vbscript:

This is my mailing 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

但不工作...

提前致谢!

推荐答案

好的,我正在更新我的问题.伙计们,在你们的帮助下,它终于开始工作了.谢谢.这是我调用 vbscript 并向其传递参数的方式:

Alright, I am updating that question I had. It's finally working, with all of your help, guys. Thanks. Here is how I called the vbscript and passed a parameter to it:

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

这是我用于邮件的 vbscript 的样子:

Here is what my vbscript for mail looks like:

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

它正在工作.*一个人可以使用相同的技术传递多个参数.

And it is working. *One can pass more than one parameters using the same technique.

这篇关于如何将参数从 Batch (.bat) 传递到 VBScript (.vbs)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆