Running PowerShell in Task Scheduler
Asked Answered
A

3

2

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.

Amoeba answered 13/1, 2017 at 12:52 Comment(1)
does powershell start at all? i guess its either a path error or an execution policy, but its hard to tell from your question as it is now. Try (1) adding the flag -noexit should help you see the error message if there is any and (2) specify full path to powershell e.g. C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exeTredecillion
B
3

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.

Bludgeon answered 13/1, 2017 at 13:42 Comment(0)
Q
0

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.

Quintal answered 13/1, 2017 at 13:58 Comment(0)
A
0

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

Amoeba answered 13/1, 2017 at 15:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.