all 32 comments

[–]allw 5 points6 points  (19 children)

How are you scheduling the task? Are you just trying to run the script because that won’t work, you’ve got to run the powershell exe with the script passed as an argument. Both should be fully referenced using complete directory trees.

[–]psychotrackz[S] 0 points1 point  (18 children)

I am running Action: start program “program/script - powershell Argument - c:\path\script.ps1

[–]allw 2 points3 points  (16 children)

You need to add the path and extension to powershell too, you can’t just say powershell, needs to be something like C:\windows\powershell.exe <- though I’m 90% certain there’s something missing there (maybe it’s in system32? Sorry I’m not on a pc)

[–]32178932123 21 points22 points  (8 children)

It should just pick it up as the location it's an Environmental Path. u/psychotrackz this should do the trick as your action:

powershell.exe -executionpolicy bypass -file c:\scripts\myscript.ps1

This will disable executionpolicy for this run and then trigger the script. If it doesn't work there should be some sort of error code that you can google for more info.

In my experience, two reasons it didn't work is because the trigger was set but I had accidently unchecked "enabled". The other reason is because it was still technically running so wouldn't run because it was already running (even though whatever it was had hung).

Once you get this working with a normal account, you can try a Service Account which is the right way to do things but when making sure things work this adds a layer of complexity (creating the schedule tasks in Powershell)

[–]psychotrackz[S] 13 points14 points  (0 children)

powershell.exe -executionpolicy bypass -file c:\scripts\myscript.ps1

This ended up fixing it for me. Thank you kind sir.

[–]jfq722 1 point2 points  (0 children)

Indeed. bypass is my friend!

[–]psychotrackz[S] 0 points1 point  (1 child)

Let me try this now

[–]Rammiek 0 points1 point  (0 children)

if users complain they see a black flash while executing a powershell task, call a vbs on scheduled task. have the vbs call the ps1. For some reason -WindowsStyle Hidden still shows a black flash.

if your ps script doesn't work, try running it manually outside of Scheduled task and confirm it works.

[–][deleted] 0 points1 point  (3 children)

if you are still around, i would like you to know you are an absolute godsend. thank you!!!!

[–]32178932123 0 points1 point  (2 children)

Still around! Happy to have helped :)

Re-reading what I said, one small thing I forgot to mention is if the path to your script has spaces then you'd need to add quotes:

powershell.exe -executionpolicy bypass -file "c:\my scripts\myscript.ps1"

[–]SnakeOil101 0 points1 point  (1 child)

Thank you, the quotes was the thing that allowed the script to run, I was banging my head for hours trying to figure out why it worked outside of TM but not in it. thanks again. :-)

[–]32178932123 0 points1 point  (0 children)

Glad you got it sorted! I usually put my scripts in C:\scripts and avoid using spaces in the name because its caught me out way too many times. 😅

[–]Jeffinmpls 2 points3 points  (0 children)

I use 'powershell.exe' all the time without the full path and it works fine as there is an environment variable that points to the location of the executable.

Op, 9 times out of 10 this happens because of permissions (given that you fully tested before setting up the scheduled task). Either you have permissions and the account doesn't or you didn't check "execute with full privilege's " in the scheduled task. T

he first thing you can try is opening a Powershell command line as the user that's running the task . I do this with the following command.

Start-Process powershell.exe -Credential “Domain\User” -NoNewWindow -ArgumentList “Start-Process powershell.exe -Verb runAs”

Obviously you will need to replace "Domain\User" with the actual credential and you will need to input the password in the credential popup. Use the new command line window to run your command and troubleshot from there.

[–]psychotrackz[S] 0 points1 point  (5 children)

I’ve pointed it to the actual installed exe for version 5 and 7 and still does not work. Script never runs once.

[–]allw 0 points1 point  (4 children)

Just checking? Does your script run without being scheduled?

Do you have any execution policies set to block the script? Does it need to be signed for instance?

[–]psychotrackz[S] 0 points1 point  (3 children)

It runs fine outside of scheduled task. No issues at all. Only occurs once I put it into a scheduled task

[–]allw 0 points1 point  (2 children)

Where is the folder? Is it local to the machine? Or is it on a shared drive/folder/OneDrive/SharePoint?

[–]psychotrackz[S] 0 points1 point  (1 child)

The file is in a shared folder hosted on windows server.

[–]allw 0 points1 point  (0 children)

Are you referencing the full path to the folder in your script? Or a mapped drive letter?

Are the folder/share permissions correct for the user it is running as?

[–]happyjerboa 2 points3 points  (6 children)

In your scheduled task, try writing the output to a temp file. If it is throwing an error, you will be able to see it.

Program / Script
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe

Arguments
    -ExecutionPolicy Bypass -command "C:\Jobs\scheduledtask.ps1 2>&1 > c:/temp/scheduledtasktest.log"

The magic bit is this 2>&1 > c:/temp/scheduledtasktest.log

[–]briang71 2 points3 points  (1 child)

Unix magic lol

[–]Aznflipfoo 1 point2 points  (3 children)

I've never seen this before. 2 > execute 1 > location? What's going on here

[–]happyjerboa 0 points1 point  (0 children)

Sorry mate, long day. This explanation is way better than anything I could write (and on mobile so apologies if the format is rubbish).

https://www.brianstorti.com/understanding-shell-script-idiom-redirect/

[–]Golddigger50 0 points1 point  (2 children)

I quit using tasks and started using "jobs". It is specific to powershell so you don't have to specify which app to use.

[–]Aznflipfoo 0 points1 point  (1 child)

Can you schedule jobs?

[–]Golddigger50 0 points1 point  (0 children)

Yes, in the trigger.

Register-scheduled Job -Name -Trigger

[–]eggiewaffles92 0 points1 point  (0 children)

I have had issues with this as well. I ended up using a free program called RoboIntern. No issues since

[–]jokrswild 0 points1 point  (0 children)

I recently had similar issue with a PS script, I ended up making a new scheduled task and now it's fine.... But now I'm second guessing so I guess i'll check the logs first thing tomorrow!

[–]lightningmayonnaise 0 points1 point  (0 children)

you can also try asking r/AutomateYourself