0

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.

Errorum
  • 223
  • 4
  • 16
  • maybe check if there's a windows service you need to have enabled for this to work? – Warren P Sep 19 '17 at 20:49
  • try again with elevated powershell? – Warren P Sep 19 '17 at 20:49
  • Are you running the scheduled job under an account with the appropriate permissions? – Preston Martin Sep 19 '17 at 20:50
  • @WarrenP What service are you thinking of? – Errorum Sep 19 '17 at 20:59
  • 1
    @PrestonM I just edited my question, yes I am running this as an admin – Errorum Sep 19 '17 at 20:59
  • Did you look in the Windows Task scheduler ? Does the task appears. Do your command / triggers looks ok ? If you right-click the job (in task scheduler) then click Run, does it execute ? – Sage Pourpre Sep 19 '17 at 21:15
  • @SagePourpre As I mentioned in the question, nothing about my new task appears in the Task Scheduler GUI at all. – Errorum Sep 19 '17 at 21:19
  • 1
    What about this ? https://blogs.technet.microsoft.com/heyscriptingguy/2013/11/23/using-scheduled-tasks-and-scheduled-jobs-in-powershell/ 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 Sep 19 '17 at 21:24
  • Try just invoking schtasks.exe from powershell. https://msdn.microsoft.com/en-us/library/windows/desktop/bb736357(v=vs.85).aspx – Warren P Sep 19 '17 at 23:45

4 Answers4

2

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
Alex
  • 498
  • 2
  • 8
  • Unfortunately, I only have the scheduledjob module, not scheduledtask. Further, I've tried a similar approach with my cmdlets, but it errors out and tells me I need to point directly to a PS1 file. – Errorum Sep 19 '17 at 22:30
  • are you using powershell 2.0? Because the "ScheduleTasks" module is pre-loaded in powershell and don't need to import any modules. – Alex Sep 19 '17 at 22:35
  • I'm using PS 5.0. I have double and triple checked that I do not have the scheduledtask module. – Errorum Sep 19 '17 at 23:01
  • See that's weird, because i've had that before, but don't know what made it different. Try this ( because i just did it and it worked no problem) open up a powershell and do `Start-process Powershell -verb runas` and then in that admin window do `ise`. Then run the script that you have with the substituted path in quotes and run. – Alex Sep 20 '17 at 03:28
  • Also since you are running on a server 2008, make sure that you don't have any script restriction and make sure `$ErrorActionPreference = "Continue"` is set to make sure you are getting those alerts if they are coming up. – Alex Sep 20 '17 at 03:31
  • That didn't change anything, got the same timeout error. I'm gonna try this with a batch script... – Errorum Sep 20 '17 at 18:19
0

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...

Kory Gill
  • 6,993
  • 1
  • 25
  • 33
0

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.

Warren P
  • 65,725
  • 40
  • 181
  • 316
  • I appreciate that you're trying, but I'm 1000% sure I've correctly right-clicked and run as admin... as stated above. – Errorum Sep 19 '17 at 23:45
0

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.

Errorum
  • 223
  • 4
  • 16