I'm attempting to write a Powershell script that runs once a day. One of the many functions it will perform is to make sure that the script itself is up to date. My problem is that since I version the scripts, I need to update the scheduled task I create from within the script.
I've thought about 2 different approaches here, of which I can't figure out either:
- I initially thought I could simply get the ScheduledTask object for my Task, and update the file path there. I was ultimately unable to access the path.
- My next thought was I could simply call
Unregister-ScheduledTask -TaskName "mytask"and then afterwards Register-ScheduledTask and recreate it with the new path.
After failing Option 1, I'm stuck on the execution of Option 2. I am able to find and Unregister my Task, but when attempting to register it with the new path I receive the following error:
Register-ScheduledJob : The scheduled job definition <myTask> already exists in the job definition store.
At line:3 char:1
+ Register-ScheduledJob -Name "<myTask>" -FilePath "C:\Apps\\Uploade ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Microsoft.Power...edJobDefinition:ScheduledJobDefinition) [Register-ScheduledJob], ScheduledJobException
+ FullyQualifiedErrorId : CantRegisterScheduledJobDefinition,Microsoft.PowerShell.ScheduledJob.RegisterScheduledJobCommand
Edit: To clarify, my Scheduled task path is something like, C:\Apps\myscript_v0.0.1.ps1 As a result, when I update, it will become C:\Apps\myscript_v0.1.5.ps1 or whatever, and I need to run the new version as opposed to old one that is currently the target.