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

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

问题描述

如何将参数从批处理传递到vbscript?我的批处理脚本在自动执行结束时发送一封电子邮件.为此,它使用/调用我的vbscript(email.vbs),该vbscript发送带有实际日志文件(具有执行结果的文件)的电子邮件. 所有这些日志文件都存储在特定的文件夹中,例如: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)

这是我现在拥有的一部分.它正在检查日志文件中的错误,并相应地调用成功/失败的邮件.现在,您在此处看到的那两个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.

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

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