0

I have a PS script that opens Excel (Com Object), processes a bunch of information, re-saves, and then sends some critical information via e-mail. The script runs great, and when I run it from the Run Console, it works great as well. However, when I schedule it as a task in the Task Scheduler it is not working properly. The task seems to "successfully run" every single time, but I do not get the output e-mail that I am supposed to get. I have run many other PS Scripts without a problem using the same configuration in the task scheduler. Could this have something to do with opening Excel as part of a script scheduled? Any thoughts are welcome.

Casousadc
  • 147
  • 1
  • 5
  • 16
  • Does the PS script need to run as an administrator? – Anthony Forloney May 15 '15 at 13:05
  • Alternately, is the script trying to access something on network drives? Try adding some basic logging to the script. Is the script running under credentials that don't have the authority to send mail from or to the addresses used in the script? – Bacon Bits May 15 '15 at 13:18
  • @AnthonyForloney - Yes, and it is running as Administrator. I am running it with the same settings as any other PS script. – Casousadc May 15 '15 at 13:23
  • @BaconBits It is trying to access something on network drives...But it can access it without a problem when I Run from the Run Console in Windows. Does it make sense that the problem is only present when ran from Task Scheduler? – Casousadc May 15 '15 at 13:24
  • 7
    Background tasks don't get network drives because the Task Scheduler uses Session 0 isolation. [Here](https://support.microsoft.com/en-us/kb/937624) is a KB article describing it, and [here](http://blogs.technet.com/b/askperf/archive/2015/02/18/help-my-scheduled-task-does-not-run.aspx) is a good blog explaining the issue (among others). You *may* be able to get around the issue with UNC paths, but if you're using administrative shares like `\\remotesystem\C$\[...]` you can still run into problems, IMX. Configuring an actual share and configuring permissions will let you use UNC paths. – Bacon Bits May 15 '15 at 13:44
  • Also Excel (Com Object) will not execute as a background task. It requires an interactive session. – Jan Chrbolka May 18 '15 at 04:59

1 Answers1

0

I solved this issue using the answer from @briantist in PowerShell script won't execute as a Windows scheduled task, but I wanted to isolate exactly which switch was solving the problem.

It had nothing to do with -ExecutionPolicy, -Noninteractive, -NoLogo, -NoProfile or any other system privilege, user account running the script, etc.

Just needed to add -File in front of the script path in the Task Scheduler > Actions > Arguments field. Without this switch PowerShell was launching and the task history was showing Action Completed, but the script was not executing.

McQuestion
  • 51
  • 1
  • 9