6

We have some scripts that create scheduled jobs using PowerShell as part of our application. When testing them recently, I noticed that some of them always failed immediately, and no output is ever produced (they don't even appear in the Get-Job list).

After many days of tweaking, we've managed to isolate it to any jobs that are set to run weekly. Below is a script that creates two jobs that do exactly the same thing. When we run this on our domain, and provide credentials of a domain user, then force both jobs to run in the Task Scheduler GUI (right-click -> Run), the daily one runs fine (0x0 result) and the weekly one fails (0x41306).

Note: If I don't provide the -Credential param, both jobs work fine. The jobs only fail if the task is both weekly, and running as this domain user.

I can't find information on why this is happening, nor think of any reason it would behave differently for weekly jobs. The "History£ tab in the Task Scheduler has almost no useful information, just "Task stopping due to user request" and "Task terminated", both of which have no useful info:

Task Scheduler terminated "{eabba479-f8fc-4f0e-bf5e-053dfbfe9f62}" instance of the "\Microsoft\Windows\PowerShell\ScheduledJobs\Test1" task. Task Scheduler stopped instance "{eabba479-f8fc-4f0e-bf5e-053dfbfe9f62}" of task "\Microsoft\Windows\PowerShell\ScheduledJobs\Test1" as request by user "MyDomain\SomeUser" .

What's up with this? Why do weekly tasks run differently, and how can I diganose this issue?

This is PowerShell v3 on Windows Server 2008 R2. I've been unable to reproduce this locally, but I don't have a user set up in the same way as the one in our production domain (I'm working on this, but I wanted to post this ASAP in the hope someone knows what's happening!).

Import-Module PSScheduledJob

$Action =
{
    "Executing job!"
}

$cred = Get-Credential "MyDomain\SomeUser"

# Remove previous versions (to allow re-running this script)
Get-ScheduledJob Test1 | Unregister-ScheduledJob
Get-ScheduledJob Test2 | Unregister-ScheduledJob

# Create two identical jobs, with different triggers
Register-ScheduledJob "Test1" -ScriptBlock $Action -Credential $cred -Trigger (New-JobTrigger -Weekly -At 1:25am -DaysOfWeek Sunday)
Register-ScheduledJob "Test2" -ScriptBlock $Action -Credential $cred -Trigger (New-JobTrigger -Daily -At 1:25am)

2 Answers 2

-1

I have just had to figure this one out myself, so here is what I found.

In the poweshell documentation I found the following:

Get-ScheduledJob gets only scheduled jobs that are created by the current user using the Register-ScheduledJob cmdlet.

http://technet.microsoft.com/en-us/library/hh849784.aspx

The impact of this is that if you create jobs with user 'A' and then run scripts trying to access them with user 'B' an error is thrown stating it can not find the job.

2
  • To clarify you need to determine what user created the weekly jobs and separate the scripts to execute under different user credentials for the creators of the original jobs. Mar 7, 2013 at 0:03
  • 1
    I don't understand why this would cause different behaviour for Weekly tasks (a crash!) vs daily tasks (which work)? Mar 7, 2013 at 14:14
-1

Since the same script adds both scheduled jobs, the possibility the difference is in the session/current user credentials is not likely to be the case.

I would swap the order in which you remove and add each job, to handle the daily job first, then the weekly job. If that changes nothing, then it is the job parameters and not something in the scheduler state engine that is perhaps uninitialized in error.

4
  • The order doesn't change anything, and the params are identical. The only difference is the frequency and user, and changing either stops the crash. Jeff Hicks asked Snover about this and he seemed to have an idea what's happening but it doesn't sound like a fix is likely. I've lost all confidence in PowerShell after many bugs like this and no help from MS (we have to rebuild our jobs every few months due to serialisation errors launching jobs!); I would absolutely not recommend using it for scheduling. Sep 8, 2014 at 10:38
  • Opps, my bad. I thought it was the same user account/credential for both jobs based on your example. I assume you manually add the jobs now. I gather you already tried to use the scheduler commandline "AT" tool (although it is probably not powerful enough to set up the credentials?) Sorry, this one has me - I am a great fan of Powershell for DEVOPS type work, and use it to provision apps onto Virtual Machines all the time.
    – Conrad B
    Sep 9, 2014 at 13:20
  • ... Is it perhaps because one of the user accounts does or does not have a "session" on the machine already? Or maybe that the problem is that the Windows scheduler powershell.exe parameters are not including things like -noprofile and -executionpolicy bypass options. I would try running my payload as a physical script and not as a scriptblock so that I could try using a manually scheduled task for C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe directly. Sorry I am late, looks like a module bug. I don't want to start a distracting technology discussion but wish you well @Danny.
    – Conrad B
    Sep 9, 2014 at 13:38
  • Running the job as the same user but different schedule works fine. This is 100% reproducible, it's not a quirk of our setup. It seems absolutely clear that it's a Windows/PoSh bug, but like every major PoSh bug we've raised; nobody at MS cares :-/ Sep 9, 2014 at 13:43

You must log in to answer this question.

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