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.

9
  • maybe check if there's a windows service you need to have enabled for this to work?
    – Warren P
    Commented Sep 19, 2017 at 20:49
  • try again with elevated powershell?
    – Warren P
    Commented Sep 19, 2017 at 20:49
  • Are you running the scheduled job under an account with the appropriate permissions? Commented Sep 19, 2017 at 20:50
  • 1
    @PrestonM I just edited my question, yes I am running this as an admin
    – Errorum
    Commented Sep 19, 2017 at 20:59
  • 1
    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. Commented Sep 19, 2017 at 21:24

4 Answers 4

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
6
  • 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
    Commented Sep 19, 2017 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
    Commented Sep 19, 2017 at 22:35
  • I'm using PS 5.0. I have double and triple checked that I do not have the scheduledtask module.
    – Errorum
    Commented Sep 19, 2017 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
    Commented Sep 20, 2017 at 3: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
    Commented Sep 20, 2017 at 3:31
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...

1
  • Sadly, I only have scheduledjobs cmdlets, not scheduledtask cmdlets
    – Errorum
    Commented Sep 19, 2017 at 23:51
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.

1
  • I appreciate that you're trying, but I'm 1000% sure I've correctly right-clicked and run as admin... as stated above.
    – Errorum
    Commented Sep 19, 2017 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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.