Running PowerShell in Task Scheduler

随声附和 提交于 2019-11-26 02:38:55

问题


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.


回答1:


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, e.g. 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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/41635118/running-powershell-in-task-scheduler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!