Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a parameter from Batch (.bat) to VBScript (.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)

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.

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 = "[email protected]"
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

But not working...

Thanks in advance!

like image 995
duper Avatar asked Jun 29 '12 15:06

duper


People also ask

How do you pass arguments in VBScript?

In VBScript, there are two ways values can be passed: ByVal and ByRef . Using ByVal , we can pass arguments as values whereas with the use of ByRef , we can pass arguments are references.

Can we pass parameters to batch file?

Batch parameters (Command line parameters): In the batch script, you can get the value of any argument using a % followed by its numerical position on the command line. The first item passed is always %1 the second item is always %2 and so on. If you require all arguments, then you can simply use %* in a batch script.


1 Answers

To answer the question...

invoke your script this way:

cscript //nologo success_mail_DEV.vbs  ARG1  ARG2

Handle arguments within vbscript via WScript.Arguments.


But may I also suggest that you could eliminate the batch portion of the system completely.

VBSCript is perfectly capable of invoking FINDSTR and handling the output. Or in fact you could implement the search wholly within VBScript with no need to invoke FINDSTR at all.

like image 116
Cheeso Avatar answered Sep 18 '22 10:09

Cheeso