I wanted to be able to send an email from a step in a SCCM 2012 OS Deployment (OSD) task sequence. This was so that I could be notified when the task sequence had completed successfully.
The account used to send the email needed to be a specific account, as otherwise Exchange would reject the message. I’d already created a firstname.lastname@example.org account in Active Directory and a mailbox for it in Exchange.
I thought I’d just use a Run Command Line step, and run PowerShell.exe specifying a very basic script file on the command line containing a Send-MailMessage cmdlet. That fails to run with an error though, and the Run PowerShell Script step doesn’t have a “Run this step as the following account” option.
So I reverted to “good old” VBScript. This is a script that sends an email using CDO.Message:
Const cdoNTLM = 2 'NTLM Set oMessage = CreateObject("CDO.Message") Dim sComputerName, sOSDComputerName, sSubject, sTextBody Dim oShell Set oShell = CreateObject("WScript.Shell") sSubject = "Build Complete %COMPUTERNAME%" sSubject = oShell.ExpandEnvironmentStrings(sSubject) oMessage.Subject = sSubject sTextBody = "Build Complete "&Now oMessage.TextBody = sTextBody oMessage.From = "email@example.com" oMessage.To = "firstname.lastname@example.org" oMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 oMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.rcmtech.co.uk" oMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 oMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoNTLM oMessage.Configuration.Fields.Update oMessage.Send
Save that into a file called EmailBuildComplete.vbs.
I then put this script file into an SCCM Package, and distributed it to my distribution points. At the end of the OSD task sequence, I then added a Run Command Line step called Send Email Notification, containing the following command line:
cscript.exe //nologo EmailBuildComplete.vbs
and set the step to run as my sccm.notifications account.