VBScript will not produce result using task scheduler
-
Friday, November 30, 2012 9:37 PM
I have worked on this for about 8 hours and have tried a ton of advice from lots of forums, but still cannot get this working.
I have a script that creates and sends an email when it is run. When run from the command prompt everything works beautifully. When I run it from the task scheduler the log shows it ran successfully, but I receive no email. (This code sits on a Windows 7 server and the Administration account is always logged in.)
Here is what I have tried:
-- Task has been created and is run by Administrator account. -- Run if logged in or not, Run with the highest privileges
-- Program Script = C:\Windows\system32\cscript.exe -- Arguments = "C:\CSO.vbs" -- start in = C:\
-- I looked at the task scheduler properties (local computer) and the Log On is set to Local System Account, but Allow services to Interact with desktop is NOT checked. This is greyed out and I do not know how to check it.
-- Under Active Tasks in the task scheduler is says the task location = \
-- I went to UAC and do not see anything wrong here, but may not know what I need to look at.
Here is the script that is called:
'Creates a new e-mail item and modifies its properties'Dim olApp
Set olApp = CreateObject("Outlook.Application")
Dim objMailSet objMail = olApp.CreateItem(olMailItem)
With objMail
.Subject = "Fuel Inventory 178"
.Body = "EJ Pope Inventory file for HM163 and HM144"
.Recipients.Add("email address")
.Attachments.Add("C:\CSO Outgoing\Fuel Inventory 178.txt")
.Recipients.ResolveAll
.display
.Send
End WithDo I need to add something to my script? Do I have a permission problem? Does anyone have any insights?
Thanks,
Cindy
All Replies
-
Friday, November 30, 2012 9:41 PMModerator
Hi,
Microsoft Office apps are difficult to automate in scheduled tasks. I recommend using a command-line utility such as blat or a PowerShell script that uses the send-mailmessage cmdlet. Both of these approaches use SMTP to talk directly to the mail server, so your mail administrator may need to configure the SMTP server to accept mail submissions from the machine you're using.
Bill
- Marked As Answer by CindyLa Wednesday, December 05, 2012 8:38 PM
-
Friday, November 30, 2012 10:14 PM
Instead of using Outlook to send your mail you could use this code:
const cdoBasic=1
schema = "http://schemas.microsoft.com/cdo/configuration/"
Set objEmail = CreateObject("CDO.Message")
With objEmail
.From = "James@company.com"
.To = "Jim@company.com"
.CC = "Boss@company.com"
.Subject = "Test Mail"
.Textbody = "The quick brown fox " & Chr(10) & "jumps over the lazy dog"
.AddAttachment "d:\Testfile.txt"
With .Configuration.Fields
.Item (schema & "sendusing") = 2
.Item (schema & "smtpserver") = "mail.company.com"
.Item (schema & "smtpserverport") = 25
.Item (schema & "smtpauthenticate") = cdoBasic
.Item (schema & "sendusername") = "James@company.com"
.Item (schema & "smtpaccountname") = "James@company.com"
.Item (schema & "sendpassword") = "SomePassword"
End With
.Configuration.Fields.Update
.Send
End With
- Proposed As Answer by jrvMicrosoft Community Contributor Friday, November 30, 2012 10:40 PM
-
Monday, December 03, 2012 5:15 PMModerator
Oberwald's code will also work, provided the script can access the schema via HTTP. Same caveat about SMTP server security applies.
Bill
-
Wednesday, December 05, 2012 4:45 PM
I have been working on getting this running and have hit a snag.
I installed PowerShell 3.0 on my 64-bit Windows 7 (SP1) server - download file was Windows6.1-KB2506143-x64.msu I let the download default the location of the files. I do not see a c:\script folder. PowerShell executables reside in C:\windows\winsxs
I changed the execution policy so I could run my script.
At the command prompt I typed:
powershell -noexit .\PowerCSOemail.ps1
PowerShell found the file and started to process it. On every line it returns that "the term 'X' is not a recognized cmdlet....." I looked this up and only find where this error is from a misspelling or forgetting the .\ I also looked up command differences between PowerShell 2.0 and 3.0, but I don't think that is it.
Would you guys know why every cmdlet is not recognized?
Thanks,
Cindy
-
Wednesday, December 05, 2012 4:52 PMModerator
And the script file PowerCSOemail.ps1 file contains...?
And the exact error messages are...?
(Remember, we can't see your screen!)
Bill
-
Wednesday, December 05, 2012 5:47 PM
Sorry. It is the script from above....Error messages are bottom...the error messages go on for every line of code.
const cdoBasic=1
schema = "http://schemas.microsoft.com/cdo/configuration/"
Set objEmail = CreateObject("CDO.Message")
With objEmail
.From = "X@ejpope.com"
.To = "X@ejpope.com"
.CC = "X@ejpope.com"
.Subject = "Fuel Inventory 178"
.Textbody = "EJ Pope Inventory file for HM163 and HM144"
.AddAttachment "C:\CSO Outgoing\Fuel Inventory 178.txt"
With .Configuration.Fields
.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 3
.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail server"
.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "X@ejpope.com"
.Item ("http://schemas.microsoft.com/cdo/configuration/smtpaccountname") = "X@ejpope.com"
.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "X"
End With
.Configuration.Fields.Update
.Send
End With
-
Wednesday, December 05, 2012 5:53 PMModerator
Hi,
That script is not a PowerShell script but a VBScript script. You must save that script with a .VBS extension and run it using cscript.exe or wscript.exe.
Bill
- Edited by Bill_StewartMicrosoft Community Contributor, Moderator Wednesday, December 05, 2012 5:54 PM clarification
-
Wednesday, December 05, 2012 6:50 PM
Well, as you can tell I am a novice at this stuff. I now see that Oberwald's code was to eliminate Outlook from the process...which is not a problem.
OK, back to my original problem. My .vbs code runs fine from the command prompt. It only 'appears' to run fine from the task scheduler. I do not have any debug statements inside the vbs to see error output in a file (because I don't know how to do that).
I guess I need to take my code and convert it to PowerShell. I will attempt to do that, but if you can provide any help it would be much appreciated.
Thanks,
Cindy
-
Wednesday, December 05, 2012 7:05 PMModerator
So you are saying the script you just posted works fine from a command prompt? (cscript scriptname.vbs) If so, there's not a need to convert to PowerShell.
If your script works fine from a command prompt, then it sounds like you don't have a scripting question but rather a question about how to run a VBScript from the task scheduler.
You should be able to set the program as cscript.exe and your script file as a parameter. If the script's path contains spaces, make sure to enclose it in quotes. For example:
Program/script: C:\Windows\system32\cscript.exe Add arguments (optional): "C:\My Scripts\Test Script.vbs"
You will likely need to experiment with the different scheduler settings.
Bill
-
Wednesday, December 05, 2012 8:18 PM
In the task scheduler I orginally put:
Program Script = C:\Windows\system32\cscript.exe
Arguments = "C:\CSO.vbs"
start in = C:\
and it didn't work (well, it says it worked)
Since it was so EASY I created the PowerShell. It also works fine from the command prompt, but when I put it on the task scheduler it bombs (well, again it says it works)
I am thinking it has something to do with permissions??? Running the task with highest privileges checked. I even tried putting -noexit in the task scheduler so I could see the errors, but the task fails when it is in there. Any other way to 'see' why it is completing the task, but not producing the result?
Cindy
-
Wednesday, December 05, 2012 8:37 PM
Oh...wow. I forgot to 'start in C:\' when I made the task to run the powershell. When I added 'start in' the script ran perfectly.
The real trick to this was using PowerShell.
Thank you for all your help!
Cindy

