0
votes

I'm trying to automate the process of configuring scheduled tasks so I can create the same task on dozens of computers. Right now, I have a very simple script that should add a job:

import-module PSScheduledJob
$trig=new-jobtrigger -Once -At ((get-date).addminutes(1))
Register-scheduledjob -FilePath <Test_File.ps1> -Name "Tester" -Trigger $trig

The test file works fine when run from the ISE, but does nothing after I wait for the scheduled task to work. Also, nothing appears in the Task Scheduler GUI, or when I use the get-job cmdlet. Running get-scheduledjob though, I do see the scheduledjob object just like it should be; seems like there's a disconnect somewhere.

Finally, I can't download any additional modules in my environment, otherwise I'd try other routes.

I am running this as an admin.

Update: I do now see the task in task scheduler GUI. However whenever it runs, it times out before doing anything. Still unsure why it's not able to call my script though.

4
maybe check if there's a windows service you need to have enabled for this to work? - Warren P
try again with elevated powershell? - Warren P
Are you running the scheduled job under an account with the appropriate permissions? - Preston Martin
@PrestonM I just edited my question, yes I am running this as an admin - Errorum
What about this ? blogs.technet.microsoft.com/heyscriptingguy/2013/11/23/… I do have some experience with Register-ScheduledTasks but not Jobs... From the link above, it is mentionned Reigster-ScheduledJobs does goes in the Microsoft/Windows/Powershell/ScheduledJobs path from the task scheduler. - Sage Pourpre

4 Answers

2
votes

Try this... It seems that you aren't pointing to powershell, because the script is only used as an argument. The action you are trying to schedule is to run powershell and the script is the argument.

$resumeActionscript = '-File "c:\ScriptPath\Script.ps1"'
$act = New-ScheduledTaskAction -Execute "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Argument $resumeActionscript
$trig = New-ScheduledTaskTrigger -Once -At ((get-date).addminutes(1))
Register-ScheduledTask -TaskName ResumeWFJobTask -Action $act -Trigger $trig -RunLevel Highest
0
votes

Perhaps this answer will help you with all the various settings you may need to achieve your desired results:

Powershell run job at startup with admin rights using ScheduledJob

Tailor the settings accordingly to your needs...

0
votes

There's no clear documentation out there for that set of commands, and the blog posts I could find all are filled with syntax errors and have unusable results.

My guess is you didn't properly Elevate (right click run as Administrator), or that you need to make the task launch powershell as the executable, not .ps1 files.

0
votes

This isn't directly answering my original concern, but I think people viewing this post will appreciate this answer. Use batch scripting instead of powershell to create your scheduled tasks. This line of code worked perfectly:

schtasks /create /tn "Tester" /tr "powershell 'Path_to_PS1'" /sc daily /st 19:00:00

Zero problems, at least for this simple use.