Configure MDT to send an email when deployment is completed

A while ago me and fellow MVP Mikael Nystrom created a small vbscript that sends an email when the deployment is completed… Here it is  🙂

Download ZTISendmail

Update 2011-06-30: Added the testmail.vbs script for troubleshooting purposes, this is just the CDO.Message code, no MDT involved at all. Run via cscript.exe testmail.vbs to verify your smtp server is correctly configured.

Instructions

  1. Download and extract the ZTISendmail.zip file to a folder, say D:\ZTISendMail  
  2. Using Deployment Workbench, create an application using the following settings

    Application with source files
    Application name: ZTISendmail
    Source Directory: D:\ZTISendMail
    Specify the name of the directory that should be created: ZTISendmail
    Command Line: cscript.exe ZTISendmail.wsf
    Working directory: .Applications\ZTISendmail   

  3. Add the ZTISendmail application as the final step of the task sequence (se below screenshot)  
  4. Configure your rules (customsettings.ini) using the sample customsettings.ini file (content below).
[Settings]
Priority=Default, SendMail
Properties=OSDSendMailFrom,OSDSendMailTo,OSDSendMailSubject,OSDSendMailBody,OSDSendMailSMTPServer,OSDSendMailIncludeBDDLog

[Default]
_SMSTSOrgName=%OSDComputername% soon to be in service...
OSInstall=Y

[SendMail]
[email protected]
[email protected]
OSDSendMailSubject=Mail from MDT01
OSDSendMailBody=The computer: %OSDComputerName% was installed at: #Now()#
OSDSendMailSMTPServer=mail.deploy.net
OSDSendMailIncludeBDDLog=YES

/ Johan

About the author

Johan Arwidmark

5 1 vote
Article Rating
Subscribe
Notify of
guest
56 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
jeremyts
jeremyts
6 years ago

Yep, the UserID is encoded using base 64. You need to decode it before passing it to a function. To do this you can simply more or less copy the six Base64 Functions directly from the Microsoft ZTIUtility.wsf script. However, this particular ZTISendMail.wsf script no longer works under WinPE 4.x/5.x due to the missing CDO object. Instead of wasting time trying to patch and force MDT to work in an unsupported way, it's easier to move to the new PowerShell script provided by Mikael Nystrom: deploymentbunny.com/2014/06/13/osdusing-ztisendmail-to-send-email-in-a-ltizti-task-sequence/ Alternatively, as Johan pointed out MDT 2012 and above allows you to create events… Read more »

Admin
Admin
7 years ago

Hi Vargar,

I don't remember seeing MDT obfuscating userID (only UserPassword)… Send me an email and I can followup offline, contact info is in the About page.

/ Johan

Vargar
Vargar
7 years ago

I'm trying to use the UserID TS variable in my MDT2013 Task Sequence. The entry above and the newer article from Mikael, deploymentbunny.com/2014/06/13/osdusing-ztisendmail-to-send-email-in-a-ltizti-task-sequence/, seems to use the UserID variable. My issue: I put in my user name and password at the beginning and everything works great. I look in BDD.log after the deploy and 'Property UserID is now = j2e', my correct ID. I was trying to use the %UserID% variable at two spots. One: an if statement, only run this task if TS UserID = Ned Two: in a vbscript at the end to write to a database logging… Read more »

Mattster
Mattster
7 years ago

Thanks Johan.
That did the trick!

Admin
Admin
7 years ago

The order looks ok so it's most likely something also, but if you want to hardcode the script to read the current variable, you can

Change line 58 from

emailObj.TextBody = oEnvironment.item("OSDSendMailBody")

to

emailObj.TextBody = "The computer: " & oEnvironment.item("OSDComputerName") & " was installed at: " & Day(Now()) & "/" & Month(Now()) & "/" & Year(Now())

/ Johan

Mattster
Mattster
7 years ago

Thanks Johan,
I am running this order: Priority=Model, TaskSequenceID, DefaultGateway, Default, SendMail with,
Properties=MyCustomProperty,ComputerLocationName,OSDSendMailFrom,OSDSendMailTo,OSDSendMailSubject,OSDSendMailBody,OSDSendMailSMTPServer,OSDSendMailIncludeBDDLog
So I guess this does not work?
Can you let us know how to do the following: You can also change the script to read the computername directly from the task sequence variables, instead of reading from the initial rules??
Kind Regards,

Admin
Admin
7 years ago

As long as the Sendmail section is called after the DefaultGateway section you should be fine. You can also change the script to read the computername directly from the task sequence variables, instead of reading from the initial rules.

As far as changing the date format, you can format the OSDSendMailBody string anyway you like, the following will change the date to UK style (dd/mm/yyyy)

OSDSendMailBody=The computer: %OSDComputerName% was installed at: #Day(Now()) & "/" & Month(Now()) & "/" & Year(Now())#

/ Johan

Mattster
Mattster
7 years ago

Hi Johan,
This works for me but not as expected.
I set a ComputerLocationName based on DefaultGateway in my customsettings.ini – e.g. MA then fill out the rest when deploying MACF1129
The sendmail sees the OSDComputerName as MA for the initial couple of checks before registering the full computer name MACF1129 later on.
The email is received as: The computer: MA was installed at: 4/11/2014 4:04:37 PM
What I expect is The computer: MACF1129 was installed at:11/4/2014 4:04:37 PM.

Is there a way to resolve this otherwise great script? Also note the date, can it be changed to UK?
Thanks,

Admin
Admin
9 years ago

If you upgrade to MDT 2012, you can simply enable monitoring which will create events on the deployment server. Those events can be be configured (via Task Scheduler) so send an email.

/ Johan

mrklintan
mrklintan
9 years ago

Do you have a solution to send email regardless of deployment summary status? Currently your solution will only send the email if the task step is run. For a deployment failure it will not be sent. Currently I've changed LTICleanup to send the email but I was hoping there's a solution that will not require to modify original MDT scripts. Perhaps Michael Niehaus and his team will implement this (email summary status) in future release of MDT.

marshal.anson
marshal.anson
9 years ago

Is SMTP authentication still not possible?

Admin
Admin
10 years ago

Have the wizard set a custom property (variable) and refer to that variable in the script

/ Johan

pgriggs
pgriggs
10 years ago

Hey

The script works fine I have created a page on the wizard to input email address so technician 1 builds a machine and recieves the email for that machine at moment it goes to one email address, i need to know how to pass this data inputted in the wizard to the OSDSendMailto:

Please Help Thanks !!!

jhiltabidel
jhiltabidel
10 years ago

I should add … to test this I removed McAfee from being installed during deployment and the email worked fine. After that test and verifying a port block had been initiated in the McAfee log files, I contacted someone in systems admin and they made the McAfee exception for me.

jhiltabidel
jhiltabidel
10 years ago

In our case, McAfee will stop this script cold unless an exception is made on the server end. My earlier comment about SMTP authentication is moot. Once we configured McAfee to allow the script to send an email it works great! Thanks again, Johan!

mhouston100
mhouston100
10 years ago

What a monkey… I didn't have the quotes around the text! I'll give it another hit today, thanks for that!

Admin
Admin
10 years ago

In my testing adding vbcrlf works just fine… Did you try the following?

emailObj.TextBody = "Line1" & vbcrlf & vbcrlf & "Line 3"

/ Johan

mhouston100
mhouston100
10 years ago

Ok so just tested it, seems that it doesn't work. Anyone have any ideas on formatting the body of the email?

Its not really a top priority but would be helpful.

mhouston100
mhouston100
10 years ago

Running like a dream now your a lifesaver!

I'm just customising the subject and body of the email now, is it possible to add a linebreak into the body? Is it just read as a normal VB string?

In other words would adding '& vbCrLf &' add a linebreak?

Admin
Admin
10 years ago

Download the sample file again. For troubleshooting purposes I have included a testmail.vbs script that you can run directly on your machine to verify if your email configuration is correct. The testmail.vbs (run it via cscript.exe) is just using the CDO.Message object, no MDT involved at all.

/ Johan

acer2220
acer2220
10 years ago

Still no luck, not sure what i am doing wrong 🙁
ZTI ERROR – Unhandled error returned by ZTISendEmail: At least one of the From or Sender fields is required, and neither was found.
(-2147220979 0x8004020D)

acer2220
acer2220
10 years ago

Still no luck, not sure what i am doing wrong 🙁
ZTI ERROR – Unhandled error returned by ZTISendEmail: At least one of the From or Sender fields is required, and neither was found.
(-2147220979 0x8004020D)

acer2220
acer2220
10 years ago

I had the app in MDT, it was still my fault for not reading more. All good now thanks.

Admin
Admin
10 years ago

Yes, you most likely did not add it as an application in MDT, but rather just copied it to the scripts folder 🙂

After creating an app in MDT, pointint to the ZTISendMail folder the script will be in DeploymentShareApplicationsZTISendMail, and the ZTIUtility.vbs script is in the DeploymentShareScripts folder by default = ….Scripts from the ZTISendMail.wsf script location.

/ Johan

acer2220
acer2220
10 years ago

I get a Windows Script Host error
line: 2
char: 35
Cannot retrieve referenced URL: ….ScriptsZTIUtility.vbs,

any idea?

Admin
Admin
10 years ago

The CDO.Message object does support authentication…

/ Johan

Admin
Admin
10 years ago

Good catch, I have updated the name of the script

/ Johan

mhouston100
mhouston100
10 years ago

HA HA I'm such a gumby, I guess that's what I get for being in a hurry.

In the instructions it says to use:

cscript ZTISendmail.wsf

but the actual script inside the zip file is ZTISendEmail.wsf (Email instead of Mail).

I'm testing it now but I'm guessing that was the problemo.

jhiltabidel
jhiltabidel
10 years ago

What about SMTP authentication? Does this script assume none is necessary?

In my environment, authentication is not necessary if you are on campus — but off campus it is required. It is authenticated by our network credentials (on campus) and not our email credentials.

Thanks,

Joe
thedecoderwheel.com

jhiltabidel
jhiltabidel
10 years ago

What about SMTP authentication? Does this script assume none is necessary?

In my environment, authentication is not necessary if you are on campus — but off campus it is required. It is authenticated by our network credentials (on campus) and not our email credentials.

Thanks,

Joe
thedecoderwheel.com

Admin
Admin
10 years ago

hmm, change the script to dump the variables to the log file and see if something is missing at the time the script is run.

/ Johan

mhouston100
mhouston100
10 years ago

Great site and super post, seems to fulfil exactly what I'm after. Unfortunately I have hit a snag. I followed the instructions (and after re-activating the final summary screen) I get the error (seems to be same as above but I dont speak french!): ERROR – Unhandled error returned by ZTISendEmail: At least one of the From or Sender fields is required, and neither was found. Though when the process stops at the final summary screen and reports the error, if I open the deployment share and run the ZTISendEmail script manually, it goes through fine and I receive the… Read more »

mhouston100
mhouston100
10 years ago

Great site and super post, seems to fulfil exactly what I'm after. Unfortunately I have hit a snag. I followed the instructions (and after re-activating the final summary screen) I get the error (seems to be same as above but I dont speak french!): ERROR – Unhandled error returned by ZTISendEmail: At least one of the From or Sender fields is required, and neither was found. Though when the process stops at the final summary screen and reports the error, if I open the deployment share and run the ZTISendEmail script manually, it goes through fine and I receive the… Read more »

mhouston100
mhouston100
10 years ago

Great site and super post, seems to fulfil exactly what I'm after. Unfortunately I have hit a snag. I followed the instructions (and after re-activating the final summary screen) I get the error (seems to be same as above but I dont speak french!): ERROR – Unhandled error returned by ZTISendEmail: At least one of the From or Sender fields is required, and neither was found. Though when the process stops at the final summary screen and reports the error, if I open the deployment share and run the ZTISendEmail script manually, it goes through fine and I receive the… Read more »

Admin
Admin
10 years ago

I have not tested the script on localized platforms (French), that might be it… What if you hardcode the values in the script?

/ Johan

pepe2626
pepe2626
10 years ago

ZTI ERROR – Unhandled error returned by ZTISendEmail: Au moins un des champs De et Expéditeur est requis et n'a pas été trouvé.

pepe2626
pepe2626
10 years ago

LOG[Property LogPath is now = C:MININTSMSOSDOSDLOGS]LOG]!>
LOG[ZTISendEmail: Send email to administrator]LOG]!>

Admin
Admin
10 years ago

What did the ZTISendmail.log say?

/ Johan

pepe2626
pepe2626
10 years ago

thanks,
ztisendemail.log say :
"Priority=Default, SendMail
Properties=MyCustomProperty,OSDSendMailFrom,OSDSendMailTo,OSDSendMailSubject,OSDSendMailBody,OSDSendMailSMTPServer,OSDSendMailIncludeBDDLog

[Default]
_SMSTSOrgName=%OSDComputername% sera bientôt opérationnel …
OSInstall=Y

SkipUserData=YES
UserDataLocation=NONE
SkipAppsOnUpgrade=NO
SkipCapture=YES
SkipAdminPassword=YES
SkipProductKey=YES
WsusServer=http://srv-mgt-1
TimeZone=105
TimeZoneName="Romance Standard Time"
SkipTimeZone=YES

[SendMail]
[email protected]
[email protected]
OSDSendMailSubject=Mail from MDT01
OSDSendMailBody=The computer: %OSDComputerName% was installed at: #Now()#
OSDSendMailSMTPServer=courrier.XUIS.fr
OSDSendMailIncludeBDDLog=YES

Admin
Admin
10 years ago

Add a pause to the task sequence (a vbscript with msgbox "OK"), and run the ztisendmail.wsf script manually to see where it errors out. You can also check the ZTISendmail.log file.

/ Johan

pepe2626
pepe2626
10 years ago

hello,
i have a problem, mdt return unexpected error code 1.
What's problem?

pempem
pempem
10 years ago

@thadkew
Now it works great..
My problem was with relaying 🙂
/Per

Admin
Admin
10 years ago

The logon dialog box in MDT does not accept logon names formated like [email protected] so you have to do a lookup.

Rather than adding ADSI to WinPE I recommend creating a small web service, pass the UserID to the web service, have the web service look up the email address from Active Directory and then return the value to MDT and use it for the email address variable in customsettings.ini

/ Johan

JamesW
JamesW
10 years ago

One question… the network credentials used to build the machine… is there any way to email "that" username @yourdomain.com…
EX: Joe Smith builds a machine, and logs in with JSmith/Password/domain.com… this script would then email [email protected]com, and Dana Rogers would be emailed at her address, [email protected]
Thanks in Advance!

pempem
pempem
10 years ago

@ thadkew
Hmmm, I have some problems with this. Could you please paste the script in its full?
Nice work, by the way 🙂
/ Per

thadkew
thadkew
10 years ago

OK OK OK… I think I figured it out…

Replace everything from the top to line 20 with this:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & strComputer & "rootcimv2")
Set objAdsSystemInfo = CreateObject("adsysteminfo")
Set objComputername = Getobject("LDAP://" & objAdsSystemInfo.ComputerName)
Set colServiceList = objWMIService.ExecQuery _
("Select * from Win32_Service where Name = 'PortBlock Service'")

strHostName = UCase(replace(objComputerName.Name,"CN=",""))

For Each objService in colServiceList
If objService.State = "Running" Then
objService.StopService()
Wscript.Sleep 5000
End If
errReturnCode = objService.ChangeStartMode("Automatic")
Next

Set objMessage = CreateObject("CDO.Message")

objMessage.Subject = "SCCM OS deployment has completed" & " " & strHostName

That will give you the PC name… The rest will attach the smts.log file and send it.

thadkew
thadkew
10 years ago

Oh, and the previous code isn't mine. I just copied and tweaked a code from ravens.blogspot.com2011/03/vbscript-send-email-with-attachment.html

And i added it to the Task Sequence as a Run Command Prompt and run it that way.

thadkew
thadkew
10 years ago

I think I found the answer. However it doesn't have the computer name. I just created a vbs file and added a task at the end of the OSD sequence. It attaches the log file too. So that's a plus. strComputer = "."Set objWMIService = GetObject("winmgmts:" & strComputer & "rootcimv2") Set colServiceList = objWMIService.ExecQuery _("Select * from Win32_Service where Name = 'PortBlock Service'") For Each objService in colServiceListIf objService.State = "Running" ThenobjService.StopService()Wscript.Sleep 5000End IferrReturnCode = objService.ChangeStartMode("Automatic") Next Set objMessage = CreateObject("CDO.Message") objMessage.Subject = "SCCM OS deployment has completed"objMessage.From = "[email protected]"objMessage.To = "[email protected]"objMessage.TextBody = "This attachement Contains the Parsed log files"… Read more »

Admin
Admin
10 years ago

I have not tested with ConfigMgr (yet) but as long as you put the ZTISendmail.wsf script in the ConfigMgr 2007 – MDT 2010 U1 Files package (scripts folder) and do the following modification it should be fine.

Change the src location in line 2 in ZTISendmail.wsf to the following

src="ZTIUtility.vbs"

/ Johan

pempem
pempem
10 years ago

@Thadkew
I'l be looking forward to hearing from you 🙂
We are running several classrooms each night, and it would be nice to know all went well, without checking several logfiles in SCCM.
/ Per


>