Scheduled Job Frustration

$creds = Get-Credential -UserName MYDOMAIN\MYACCOUNT -Message "Scheduled task creds"
$script = "SCRIPTNAME"
$scriptPath = Join-Path "D:\Scripts" "$script.ps1"

# Split username & password
if ($creds -ne $null) {
    $username = $creds.username
    $password = $creds.GetNetworkCredential().password
}

# Get your Domain
$Root = "LDAP://" + ([ADSI]"").distinguishedName
$domain = New-Object System.DirectoryServices.DirectoryEntry($Root,$UserName,$Password)

if (($creds -ne $null) -and ($domain.name -ne $null)) {
    Write-Verbose "Verified credentials for $username"
} else {
    Write-Warning "No valid credentials for $username!"
    break
}

try {
    gci $scriptPath -errorAction stop | Out-Null
}
catch {
    Write-Warning "Couldn't find this script!"
    break
}
$dailyTrigger = New-JobTrigger -Weekly -at "5:30 AM" -DaysOfWeek Tuesday,Thursday 
$options = New-ScheduledJobOption -RequireNetwork
Register-ScheduledJob -Name $script -Trigger $dailyTrigger -FilePath $scriptPath -ScheduledJobOption $options -Credential $creds

 

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!

 

I’ve not run into this. We’ll see if anyone else has.

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.

Quite the puzzler!

David

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.

David

I have same problems.

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


$cred = Get-Credential "$domain\servacc"
#daily
$joboption = New-ScheduledJobOption
$dt = New-JobTrigger -daily -At "06:00 am" -RandomDelay 00:10:00
Register-ScheduledJob -Name "ServerDaily" -cred $cred -Trigger $dt -FilePath c:\temp\daily.ps1 -ScheduledJobOption $joboption 
#weekly
$joboption = New-ScheduledJobOption -RunElevated
$wt = New-JobTrigger -Weekly -At "11:00 pm" -DaysOfWeek Sunday
Register-ScheduledJob -name "ServerWeekly"  -cred $cred –Trigger $wt –FilePath C:\temp\weekly.ps1 -ScheduledJobOption $joboption 

Looks like a bug with “-weekly + -cred”. maybe somebody can replicate this…

I encountered the same issue.
There is an open file on Connect @ https://connect.microsoft.com/PowerShell/feedback/details/776801/weekly-tasks-created-via-powershell-using-a-different-user-immediately-fail-with-error-0x41306
As far as I can see their is no solution for the moment.
Carlo

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