Link to home
Start Free TrialLog in
Avatar of FCOA
FCOAFlag for United States of America

asked on

Need alternate to CDO.Message for Windows Server Core

Looking for an alternate to CDO.Message for sending email in a VBScript on Windows Server Core.

NOTE: While we also use PowerShell scripts to send email, we have several substantial legacy VBScripts that carry out tasks and send mail, so fully rewriting these in PowerShell will be a longer term objective. In the interim, we're in need of a workaround to the VBScript issue. Thanks.
Avatar of Ben Personick (Previously QCubed)
Ben Personick (Previously QCubed)
Flag of United States of America image

The VB Script methods have not worked natively in ages, you must have installed old .net packages to get those working in 2k8/2012, so I would have thought 2016 would let you fanagle that too, but maybe they've finally removed the chance, have you looked into how you got it to work in 2012?


As for Powershell, you can just call it from inside your VBScript

Dim _SMTPServer as string = "Server IP or FQDN"
Dim _MailFrom as string = "Sending Email Address@Domain.com"
Dim _RcptTo as string = "Your Email Address@Your Domain.com"
Dim _Subject as string = "Your Email Subject"
Dim _Body as string = "Just an Example"
Dim _Attachment = "C:\temp\Completely_Optional_Attachment.ext" 

Dim _ShellSession As Object
Set _ShellSession= CreateObject("Wscript.Shell")

'Send WITH Attachment:
_ShellSession.Run("  PowerShell.exe -Command '& {Send-MailMessage -To "&_RcptTo&" -Subject "&_Subject&" -Body ( "&_Body&" | out-string )  -SmtpServer "&_SMTPServer&" -From "&_MailFrom&" -Attachment "&_Attachment&" }}' ")

'Send WITHOUT Attachment:
_ShellSession.Run("  PowerShell.exe -Command '& {Send-MailMessage -To "&_RcptTo&" -Subject "&_Subject&" -Body ( "&_Body&" | out-string )  -SmtpServer "&_SMTPServer&" -From "&_MailFrom&" }}' ")

Open in new window


I actually use this similar method from CMD as the majority of my older scripts are CMD, and I have been replacing parts that called VB Script or 3rd party utilities to send email with a call to powershell as needed.
Avatar of FCOA

ASKER

Excellent!! Thanks so much! I'll give this a try.

Interestingly, it appears the CDO.Message method works fine for us through Server 2019 on the "Desktop Experience" version, but not on Server Core. This includes devices with the base install and no add-on roles or features. This is actually our first foray into Server Core.
Avatar of FCOA

ASKER

FYI- I've been toying with the suggested VBScript, but it doesn't appear to be working. I started with it as-is, and that didn't work, then tried revising it from there. Thus far, no luck.
To be completely up front we depricated and converted all pf our VB scripting about 10 yeas ago give or take, because of inconsistamcies in each OS's implementation, and mail send cdo not working most specifically.

We've been writing pure cmd scripts and a little powershell, although its finnally flipped to mostly powershell and some cmd, so I had to lift the old school shell execute example, and had no test space.

I can say the body and subject will need quotes around them, which I don't have

 are you getting any error?  might also need to run elevated, which I recall being possiblr in vb.

finally even in CMD we usually pipe the output to a cmd script that calls the powershell, or nest the powershell in the cmd and call itself, but VBA does not work well for code nesting
Avatar of FCOA

ASKER

The following options work:

Set objShell = CreateObject("Wscript.Shell")
objShell.Run("powershell -NonInteractive -ExecutionPolicy Bypass -Command ""& { Send-MailMessage -To to@domain.com -From from@domain.com -Subject 'Test Message' -Body 'This is a test.' -SMTPServer servermame.domain.com }""")

Open in new window

OR

strSMTPServer = "servername.domain.com"
strFrom = "from@domain.com"
strTo = "to@domain.com"
strSubject = "Test Message"
strBody = "This is a test."

Set objShell = CreateObject("Wscript.Shell")
objShell.Run("powershell -NonInteractive -ExecutionPolicy Bypass -Command ""& { Send-MailMessage -To "& strTo &" -From "& strFrom &" -Subject '"& strSubject &"' -Body '"& strBody &"' -SMTPServer "& strSMTPServer &" }""")

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of FCOA
FCOA
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial