1
votes

I have written two powershell scripts that I need to run every morning. The first generates a text file of the names and location of all of the new files added to a directory within the last twenty-four hours. The second script that I wrote takes that .txt file and e-mails it to myself.

Both of these scripts run perfectly when executed manually. I added them both to the task scheduler but the scheduler does not properly execute the scripts. The text file does not get generated, and the e-mail does not get sent.

Things that I have done (for both scripts):

  1. Selected the option to run whether user is logged on or not.
  2. Set to run with the highest privleges
  3. Have checked and rechecked that my ExecutionPolicy is Unrestricted.

Here is a picture of the task scheduler at the moment

Edit: Since that picture is so small here is a direct link: http://imgur.com/EX7Vxj7

2
Under the Actions tab, can you show the action and also the Edit... details for that action? - bentek
Did you include the -noexit flag in the arguments for powershell.exe? - Mathias R. Jessen
Sure! Here's the actions tab: imgur.com/yfTMGLx and here is the edit tab: imgur.com/6RwY96w - Galen H
@MathiasR.Jessen I'm not sure what you're referring to -- would you mind expanding on the -noexit flag? - Galen H
In the "Add arguments (optional)" box. When you execute powershell.exe script.ps1, it runs the script. When you execute powershell -noexit script.ps1, it runs the script but doesn't exit the process afterwards. That doesn't seem to be the case though - Mathias R. Jessen

2 Answers

1
votes

The Action needs to be set to "Start a program" and the program should be PowerShell.exe

Secondly in the Add Arguments box you need to enter -File '[script path]'. You can add -NoExit if you want to keep the window open afterwards but it will need to be running as your own user of course.

0
votes

If your script is located on a shared map you need to provide unc to the file like this:

\\servername\script.ps1

You can find more details in this post: https://dmitrysotnikov.wordpress.com/2011/02/03/how-to-schedule-a-powershell-script/