How to pass a parameter from Batch (.bat) to VBScript (.vbs)?

后端 未结 3 1449
难免孤独
难免孤独 2021-01-01 03:13

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) t

相关标签:
3条回答
  • 2021-01-01 03:37

    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.
    

    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.

    0 讨论(0)
  • 2021-01-01 03:44

    Your VBScript needs some code to accept the arguments, for example:

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

    When you call your VBScript, just pass the arg to the script, like you would a command:

    cscript //nologo MyScript.vbs arg
    
    0 讨论(0)
  • 2021-01-01 03:51

    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.

    0 讨论(0)
提交回复
热议问题