1

I have a powershell script that creates a scheduled task using a call to RegisterTask via the Schedule.Service com object.

Essentially:

$ts = new-object -com Schedule.Service
$ts.Connect()
$rootfolder = $ts.GetFolder("\")
$taskXml = Get-Content "Task.xml"
$rootfolder.RegisterTask("\Maintenance", $taskXml, 6, "LOCAL SERVICE", $null, 5)

This works fine on my local machine (windows 7, and I am a local admin)

When I try this on the machine it will be run on, the RegisterTask call fails with ACCESS_DENIED.

However, if I run a command prompt as administrator, then powershell.exe -file myscript.sps1 it works fine and adds the task.

I have ensured that the user that it is running under has permissions to write to the Tasks folders in %windir% and %windir%/system32

The user is in the Administrators group, which is puzzling, what else do I need to do to give the user the permissions to create scheduled tasks? It seems that just adding them to the local administrators group isn't enough.

EDIT: I have logged onto the server as the user that will be running the script. I can successfully import the xml file into the Task Scheduler UI addin manually.

2
  • Did you already create the Maintenance folder?
    – Ryan Ries
    Feb 26, 2013 at 0:53
  • No, I didn't need to when running it locally. I also changed the script to create the task in the root folder and get the same issue.
    – Matt
    Feb 26, 2013 at 1:01

2 Answers 2

2

Since Powershell 4.0 you can use the ScheduledTask module to create Scheduled tasks without the use of the com-object and easier reading and writing.

Example:

$Action    = New-ScheduledTaskAction -Execute "Taskmgr.exe"
$Trigger   = New-ScheduledTaskTrigger -AtLogon
$Principal = New-ScheduledTaskPrincipal "Laptop\Administrator"
$Settings  = New-ScheduledTaskSettingsSet

$Task = New-ScheduledTask `
    -Action $Action `
    -Principal $Principal `
    -Trigger $Trigger `
    -Settings $Settings

Register-ScheduledTask -TaskName Task1 -InputObject $Task

The first command uses the New-ScheduledTaskAction cmdlet to assign the variable $Action to the executable file tskmgr.exe.

The second command uses the New-ScheduledTaskTrigger cmdlet to assign the variable $Trigger to the value AtLogon.

The third command assigns the variable $Principal to the principal of the scheduled task, Contoso\Administrator.

The fourth command uses the New-ScheduledTaskSettingsSet cmdlet to assign the variable $Settings to a task settings object.

The fifth command creates a new task and assigns the variable $Task to the task definition.

The sixth command (hypothetically) runs at a later time. It registers the new scheduled task and defines it by using the $Task variable.

Dont forget the run the code as Administrator. Source.

0

I gave up in the end, couldn't find a way to do this using register tasks, however using schtasks allowed me to import the xml file of the task without the access denied error. I could also use schtasks to import the task to a remote machine.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .