Scheduled Task rights?

by renenielsen at 2012-11-09 09:19:47

Situation scenario:

Hyper-V Server 2012
User in Domain Admins group
Domain GPO grants Log on as batch to Domain Admins group
Performed Register-ScheduledJob to register a PowerShell script with associated trigger and adjusted for RequireNetword & RunElevated as well as provided credentials of user.
Viewed scheduled task definition in Computer Manager - Scheduled Tasks under PowerShell - ScheduledJobs and all appears well with the task definition and trigger.


Success Case:

When the job’s user is logged on via remote desktop interactively or the job user’s session is in a disconnected state the scheduled task executes without an issue.


When there is no session for the user either active or disconnected the scheduled task triggers but immediately “terminates on user request”.

While I suspect this is a Scheduled Task issue I’m posting here to see if anyone has encountered this and knows what the issue is. I thought the user right - “Log on as Batch” might be the issue but I’ve tested for that.

by renenielsen at 2012-11-09 09:33:20

I created a scheduled task running on the same server and under the same user through the Computer Manager Scheduled Tasks UI and this task executes successfully regardless of whether there is a session for the user open or not.

Possibility: The issue may be related to the execution context of PowerShell ScheduledJobs.
by DonJ at 2012-11-09 12:22:44
Haven’t run into this, no. Does sound more like a SchTasks thing - can you replicate by taking PSH out of the picture and manually configuring a task with the same parameters?
by renenielsen at 2012-11-09 14:04:23
I’ll give that a try and get back.
by renenielsen at 2012-11-09 14:44:59
From the Computer Management UI I copied the PowerShell-ScheduledJobs job parameters to a new Task Scheduler Library scheduled task. I first executed this newly defined scheduled task with a disconnected session for the scheduled task account active on the host server. The task executed successfully. I then closed the disconnected session on the host server and reset the Scheduled Task trigger to instantiate a new instance of the task. This task returned a “Incorrect function (0x80070001)” result and did not execute any of the contained code. A search on this error mostly returns issues related to Backup…
by DonJ at 2012-11-09 15:22:08
Yah, sounds like it’s not PowerShell, then. Good to at least know where to focus ;).
by at 2012-11-10 05:39:06
Just a side note: Domain Admin credentials in a scheduled task seems risky to me. I might be working off outdated knowledge, but I was under the impression that one could potentially extract or at least use credentials from credential manager…
by renenielsen at 2012-11-10 11:53:39
Regarding my latest update (where I created a Scheduled Task through the Computer Manager UI), I basically cloned the PowerShell ScheduledJob Action which has a format referencing .NET/PowerShell syntax (see below).

-NoLogo -NonInteractive -WindowStyle Hidden -Command "Import-Module PSScheduledJob; $jobDef = [Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition]::LoadFromStore(‘TTProd Export’, ‘C:\Users\backupmaster\AppData\Local\Microsoft\Windows\PowerShell\ScheduledJobs’); $jobDef.Run()"

This generated the “Incorrect function” error. I have now adjusted the Scheduled Task to use the “old” powershell call syntax (which I had hoped to avoid entirely by scheduling the job using the Register-ScheduledJob PowerShell cmdlet) such as follows:

powershell -NoLogo -NonInteractive -WindowStyle Hidden -Command "F:\Exports\Hyper-VExport.ps1 TTProd"

This latest test works, the task executes and the job runs to completion, no need for the owner to be logged on…just as intended, a thing of beauty.

So the question becomes why does the PowerShell ScheduedlJob fail when the job owner is not logged in and when called as a PowerShell ScheduledJob but succeeds (without the user being logged in) when the same PowerShell script is executed as a Windows Scheduled Task (not loaded from the scheduled job definition store)?

The net difference for me is since I am scheduling jobs on a O/S without a graphic UI I had hoped to employ PowerShell’s nifty ScheduledJob cmdlets but if I can’t get this to work I’ll need to resort to managing scheduled tasks using a remote Computer Manager instance. This offends my sense of automation but I can live with it if I must. At the least I would hope that someone could either identify where I’ve gone astray or confirm that this is a defect so it could be fixed down the road.

Regarding the use of a Domain Admin account to run a batch job, absolutely correct…do not try this at home. I purposely structured my test case using Domain Admin creds to bypass suggestions that the job account owner may have been running into some permissions related issue and eliminate and entire class of troubleshooting…as much as possible. A permissions issue was in fact one of my first suspicions so I skipped to the simple case where the owner account had Godlike permissions so I could work backwards and identify the faulty permissions but alas it appears that even as a Domain Admin the job fails in a wierd manner (runs if an owner session is present but fails when run without one).
by renenielsen at 2012-11-10 20:04:09
Here is a test case, see if you get the same results I do (“The last run of the task was terminated by the user (0x41306)”). In this case the credential user is a Domain Admin to simplify possible permisions issues.

$JobTrigger = New-JobTrigger -Once -At “11/11/2012 13:00”
$Cred = Get-Credential -UserName “nielsenhome\BackupMaster”
$SJO = New-ScheduledJobOption -RunElevated -RequireNetwork

Register-ScheduledJob -Name Zulu -FilePath F:\Exports\Foo.CMD -ArgumentList “Foo2” -Trigger $JobTrigger -ScheduledJobOption -ScheduledJobOption $SJO -Credential $Cred

Where “FOO.CMD” is defined as a simple DOS copy command of one existing file to another.

For me this command does not complete (as you would assume from the Last Run Result indicated above). However when this same command procedure is called from Scheduled Tasks with corresponding task/job parameters it executes successfully. The difference, as far as I can tell, is that the PowerShell ScheduledJob is being called from the ScheduledJobDefinition store.