I’ve been having a devil of a time getting jobs to succesfully run using the PoSH V3 PSScheduledJob cmdlets. While my jobs seem to register fine, when they actually run (either manually via TaskMgr or at their scheduled time) I regularly get a message back from get-scheduledjob that the job is corrupt and has been removed. There isn’t any other information as to what PoSH thinks the problem is and I have several apparantly similar jobs that run without a hitch.
I’ve even put together a code snippet that I use to schedule my jobs, ensuring that I get all the options right, have the proper service account, etc. Does anyone see any obvious errors with my approach below or have suggestions as to how to get more info as to why my jobs are always ‘corrupt’? Thanks!
Played with this a bunch last night and can’t replicate. Those job definitions are stored as XML files on disk; any chance some other process could be snarfing them?
Thanks for the looksee, Don. Apart from standard corporate AV, there’s not much going on with this box. I see the XML files under AppData\Local\Microsoft\PowerShell\ScheduledJobs and they pass a quick notepad inspection for funkiness. There’s nothing in the event logs either.
An update - if I start the ISE as the service account and then schedule the task, it looks like the tasks run fine. I’m still using the above code and all the options are the same so I’m not sure what’s different here apart from the job XML getting created under the AppData\Local directory of the service account than my personal account.
Has anyone used the PoSH v3 scheduledjob cmdlets with the RunAs option? I don’t know if this is a unique behavior for me or if there’s something more systemic going on here.
When executing register-scheduledjob:
powershell is running with my admin account and $cred is servacc, daily works fine but weekly is corrupted every time when executed.
powershell is running with servacc and $cred is servacc, both works fine
I’ve found Scheduled Jobs to be a bit flaky and temperamental.
A scheduled job will become corrupt in a number of circumstances:
running it using the scheduled task cmdlets - almost always
running it through the task scheduler before it’s run once on a schedule - roughly 25% of the time & I can’t tie down the reasons
In PowerShell v4 things are a little better as you have the ability to start the scheduled job through the RunNow parameter for Register-ScheduledJob and Set-ScheduledJob. I’d recommend upgrading to PS 4.0 if you want to use scheduled jobs just for those parameters.
Also once your scheduled job has become corrupt, deleting it and recreating won’t work. The name is preserved somewhere - haven’t tracked it down yet - and the new definition is immediately flagged as corrupted. You need to give the scheduled job a new name