0

I have this Powershell script and I want to schedule it run every 1 minute. So I created the task and schedule it to run every. If Right-Click->Run the scripts runs OK. But when it's scheduled it says that it ran successfully but it's not. I have also checked "Run only when user is logged on" just to make sure that I can see the window when it runs but still nothing. Any ideas?

PS. The arguments line: -ExecutionPolicy Unrestricted -NonInteractive -NoProfile -File C:\dj.ps1

Screenshots: enter image description here enter image description here enter image description here

enter image description here

user776720
  • 197
  • 3
  • 12
  • Change to run whether user is logged in or not (check the tickbox in the first screen) – Mikael Dyreborg Hansen Dec 16 '16 at 12:21
  • Already tried that without luck – user776720 Dec 16 '16 at 12:24
  • 1
    Can you post the script you're running? – Mikael Dyreborg Hansen Dec 16 '16 at 12:54
  • Unfortunately not because it contains sensitive data. It performs a query on a database and then extracts the result in JSON. Have in mind that if I right-click and click RUN I can see the script executed successfully. – user776720 Dec 16 '16 at 12:59
  • Click the browse button and find the powershell exe and let it have the full path in there. In start in have the path to the script. – JBaldridge Dec 16 '16 at 13:38
  • 3
    Possible that it is being run, throwing an error, and exiting before you can see it.Add first line `start-transcript c:\temp\debug.log` and last line `stop-transcript`. Then review the log file. – Clayton Dec 16 '16 at 15:49
  • How do you know it's not working? You're not going to see an execution window when you're logged in because it runs in a different context. I strongly recommend you add logging to your script. This is kinda in the realm of a bad question as you've given us nothing to go on except "it don't work". – Colyn1337 Dec 16 '16 at 17:04
  • Some components requires an ineractive session (such as Excel) - Now knowing what you script calls and uses, I'd guess this could be a scenario. Log in with the user on the server that runs the task (RDP is good enough) and check if this resolves it for you. – Mikael Dyreborg Hansen Dec 19 '16 at 09:07
  • What is the path to your script? I have run into issues before where if your path has a space in it, the script fails without an error. You might try replacing your script with a dummy script that creates a text file, just to see if the issue is your script or the task. Task Scheduler has a lot of problems. I find new ones every time I have to use it. I recommend going 3rd party if you can. – Michael Timmerman Dec 21 '16 at 19:28

1 Answers1

1

I had the same issue (or very similar). I was able to fix it by configuring the scheduled task's action parameters like so:

  • (Program/script:) C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
  • (Add arguments) -ExecutionPolicy Bypass -File "C:\full\path\to\folder\containing\the\PSscript\script.ps1"
  • (Start in) C:\full\path\to\folder\containing\the\PSscript\

Hope this helps!

ShadowFox
  • 43
  • 7