Scheduled Job Problem

I have been struggling recently with configuring a scheduled job to run some functions I have created and placed in a module. The two functions I have created work perfectly when executed at the console and I have tested them as a background job as well and had no problems. Here is the code I am using to create the scheduled job:

$AtStartup = New-JobTrigger -AtStartup
$AtLogon = New-JobTrigger -AtLogOn
$Midnight = New-JobTrigger -Daily -At "12 AM"

$OptionParams = @{
    StartIfOnBattery = $true
    ContinueIfGoingOnBattery = $true
    RequireNetwork = $true
    MultipleInstancePolicy = 'StopExisting'
}

$JobOptions = New-ScheduledJobOption @OptionParams

$JobParams = @{
    Name = 'Something'
    ScriptBlock = {Get-Something | Set-Something}
    Trigger = $AtStartup,$AtLogon,$Midnight
    ScheduledJobOption = $JobOptions
    InitializationScript = {Import-Module MyModule}
}

Register-ScheduledJob @JobParams 

When I run this it creates the job and the triggers look ok. However, when it runs I get the following error from the job:

Receive-Job : Running startup script threw an error: The scheduled job definition Something already exists in the job definition store..
At line:1 char:11
+ get-job | Receive-Job -Keep
+           ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidResult: (:) [Receive-Job], RemoteException
    + FullyQualifiedErrorId : ScheduledJobFailedState

Any thoughts why this might be happening? Thanks!


  • Edited by Matt McNabb 7 hours 45 minutes ago Hide function name
January 20th, 2014 10:51pm

Hi Matt,

To get the output of the scheduled job you can't use the Recieve-Job cmdlet cause it is stored in a directory
Am copy-pasting the below from Scripting Guy's Blog:

 The best part of a scheduled job is that all results of all job instances are saved at the following location, and they are available to you at any time:

$home\AppData\Local\Microsoft\Windows\PowerShell\ScheduledJobs\<jobName>\Output

To get them, run:

PS C:\ps-test> dir $home\appdata\local\Microsoft\Windows\PowerShell\ScheduledJobs\Update-MyHelp

    Directory: $home\appdata\local\Microsoft\Windows\PowerShell\ScheduledJobs\Update-MyHelp

Mode                LastWriteTime     Length Name

----                -------------     ------ ----

d----        10/14/2013  10:20 AM            Output

-a---        10/15/2013   2:50 PM       7000 ScheduledJobDefinition.xml

Below is the link of the post

http://blogs.technet.com/b/heyscriptingguy/archive/2013/11/23/using-scheduled-tasks-and-scheduled-jobs-in-powershell.aspx

Hope this helps

Free Windows Admin Tool Kit Click here and download it now
January 20th, 2014 11:50pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics