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