none
Pass variable from Powershell to VBScript RRS feed

  • Question

  • How do I pass a variable from Powershell to VBScript?

    The Powershell command I have is:

    $name = "myName"
    $sendTxt = cmd /C wscript E:\go\txtMsg\email.vbs thisName:$name
    invoke-item $sendTxt

    In which I would like the following legacy VBscript to use either $name or thisName

    Set objMessage = CreateObject("CDO.Message") 
    objMessage.From = "Web.Server@WestHartfordCT.gov"
    objMessage.To = "me@myemail.com" 
    objMessage.TextBody = thisName
    objMessage.Send

    I have tried just using $name in both the Powershell and VBScript code, but $name produces an error in the VBScript code.


    Jeff Roller

    Monday, August 28, 2017 3:24 PM

Answers

  • You don't need a VBScript to send mail from PowerShell. PowerShell has a Send-MailMessage cmdlet for this purpose.


    PS C:\> help Send-MailMessage


    -- Bill Stewart [Bill_Stewart]

    Monday, August 28, 2017 3:45 PM
    Moderator

All replies

  • You don't need a VBScript to send mail from PowerShell. PowerShell has a Send-MailMessage cmdlet for this purpose.


    PS C:\> help Send-MailMessage


    -- Bill Stewart [Bill_Stewart]

    Monday, August 28, 2017 3:45 PM
    Moderator
  • I tried doing that, but ran into a lot of security issues I didn't understand. I ended up passing a value like this:

        $sendTxt = cmd /C wscript E:\surveillance\txtMsg\email.vbs $toLog
        invoke-item $sendTxt

    Jeff Roller

    And the VB script gets the data this way (see Body, WScript.Arguments.Item(0), which if you had $toLog $moreData $evenMore in the $sendTxt line of the Powershell script, it would be (0), (1), (2) index items in the script below:

    Set objMessage = CreateObject("CDO.Message") 
    objMessage.From = "me@my.com"
    objMessage.To = "8885551212@vtext.com" 
    objMessage.TextBody = "WARNING" & VbCrLf & WScript.Arguments.Item(0)
    objMessage.Send


    Wednesday, August 30, 2017 7:38 PM