I am creating a script to be placed on servers to run without user interaction that will send an email when certain criteria are met. I have the criteria script running, but I want to make the script easy to deploy and modify for each individual server.
I am trying to make the script make a call to a text file to populate the TO field in the email. The text file will have the email addresses placed one per line (I was putting a semicolon on the end of the addresses since I know multiple TO addresses are separated by semicolons)
I have tried a number of different variations on calling the script and I either get an error on the To line, or it makes it through the script and I get an error that no recipients were defined. Here is the script below:
`Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFileEmailAddresses = objFSO.OpenTextFile("EmailAddresses.txt", 1)
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set wshShell = WScript.CreateObject("WScript.Shell")
strComputerName = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
ServicesArray = Split (objFileEmailAddresses.ReadAll, vbNewLine)
For Each strService In ServicesArray
objDictionary.Add strService, strService
Next
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "[email protected]"
objEmail.To = objFileEmailAddresses
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp address"
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.Fields.Item("urn:schemas:mailheader:X-MSMail-Priority") = "High"
objEmail.Fields.Item("urn:schemas:mailheader:X-Priority") = 2
objEmail.Fields.Item("urn:schemas:httpmail:importance") = 2
objEmail.Fields.Update
objEmail.Subject = "Primary Server " & Ucase(strComputerName) & " is Rebooting Now"
objEmail.TextBody ="The primary server " & Ucase(strComputerName) & " is scheduled to reboot at this time. The server will be offline in less than one minute. .... "
objEmail.Send
objFileEmailAddresses.Close()`