简体   繁体   中英

Running PowerShell in Task Scheduler

I am using PowerShell for downloading data from email.

I want to run this process by PowerShell. When I run script like this:

D:\script.ps1

in powershell.exe it works fine.

When I schedule it in Task Scheduler nothing happens.

I tried it to Set it like Program/script:

powershell
Powershell.exe
powershell.exe

Add arguments:

-executionpolicy bypass -file D:\script.ps1
-file D:\script.ps1
-file "D:\script.ps1"

And nothing works. I'm using Windows 2008 R2.

Troubleshooting scheduled tasks is a pain in the rear, because you can't really see what's going on. These are some things you may want to check:

  • Check that your commandline works in principle, eg by running it from CMD (in your case try running powershell.exe -File "D:\\script.ps1" ). If that gives you any errors you need to fix those first.

  • If you intend to run the task as a particular user, start CMD as that user and run the same commandline to check if the user has all the permissions required for whatever the script is doing.

  • Check if your task actually terminated or if the process is still running (via Process Explorer , Get-Process , Task Manager, …).

  • Check the Last Run Result for the exit code of the command.

  • Enable the history for your scheduled tasks ( Action → Enable All Tasks History ). That will give you at least some information about what the task is doing, whether it starts at all, and if/which errors occurred. You need administrative rights to enable the task history.

  • Check the eventlog for errors/warnings correlating with the task run.

  • Add logging statements to the script you're running to record progress information. Personally I prefer logging to the eventlog, because that avoids filesystem permissions issues.

     Write-EventLog -LogName Application -Source EventSystem -EventID 100 -EntryType Information -Message 'Your log message.' 

    If you have admin privileges on the system you can register an event source of your own and use that in the above log statement instead of abusing an existing source like EventSystem :

     New-EventLog -Source MyEventSource -LogName Application 

Further help will depend heavily on the findings you got following these steps as well as your actual script code.

Few major observations which I had faced:

  1. Instead of giving only powershell.exe , try giving the full PS path C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe .

  2. Permission is one more concern. The user through which you are running the task might not have the permission to run that.

  3. Execution Policy: Make sure you are bypassing the execution policy using -ExecutionPolicy Bypass .

  4. Make sure you are running the task with Highest Privileges .

  5. Finally, through analysis of logs.

I found this site that was quite useful: http://www.microsoftpro.nl/2011/07/07/how-to-schedule-a-powershell-script-using-scheduled-tasks-in-windows-server-2008-r2/

I also changed Secure option property and it helped.

I didnt check: Do not store password and now it runs without me being logged into network.

Peace

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM