[英]Updating Scheduled Task Script from Powershell
I'm attempting to write a Powershell script that runs once a day. 我正在尝试编写每天运行一次的Powershell脚本。 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: 我在这里考虑了两种不同的方法,其中我也想不通:
Unregister-ScheduledTask -TaskName "mytask"
and then afterwards Register-ScheduledTask and recreate it with the new path. Unregister-ScheduledTask -TaskName "mytask"
,然后调用Register-ScheduledTask并使用新路径重新创建它。 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: 在选项1失败后,我坚持执行选项2.我能够找到并取消注册我的任务,但是当尝试使用新路径注册它时,我收到以下错误:
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. 编辑 :为了澄清,我的预定任务路径类似于,C:\\ Apps \\ myscript_v0.0.1.ps1因此,当我更新时,它将变为C:\\ Apps \\ myscript_v0.1.5.ps1或者其他什么,我需要运行新版本,而不是当前目标版本。
Ok, changing the job's file path is easy. 好的,更改作业的文件路径很容易。 First we get the job, then we pipe that scheduledjob object to Set-ScheduledJob and specify the -FilePath
首先我们得到工作,然后将那个Scheduledjob对象通过管道传递给Set-ScheduledJob并指定-FilePath
$NewPath = GCI C:\Apps\myscript_*.ps1 | Select -last 1 -ExpandProperty FullName
Get-ScheduledJob MyJob | Set-ScheduledJob -FilePath $NewPath
Edit: To give credit where credit is due, I would like to thank Get-Help *-ScheduledJob
followed by Get-Help Set-ScheduledJob -Full
and Example 1 for going through this exact situation... 编辑:要在
Get-Help *-ScheduledJob
地方提供信用,我要感谢Get-Help *-ScheduledJob
然后感谢Get-Help Set-ScheduledJob -Full
和示例1经历了这种确切的情况...
Example 1: Change the script that a job runs The first command uses the Get-ScheduledJob cmdlet to get the Inventory scheduled job. The output shows that the job runs the Get-Inventory.ps1 script.This command is not required; it is included only to show the effect of the script change. PS C:\\>Get-ScheduledJob -Name Inventory Id Name Triggers Command Enabled -- ---- -------- ------- ------- 1 Inventory {1} C:\\Scripts\\Get-Inventory.ps1 True The second command uses the Get-ScheduledJob cmdlet to get the Inventory scheduled job. A pipeline operator (|) sends the scheduled job to the Set-ScheduledJob cmdlet. The Set-ScheduledJob command uses the Script parameter to specify a new script, Get-FullInventory.ps1. The command uses the Passthru parameter to return the scheduled job after the change. PS C:\\>Get-ScheduledJob -Name Inventory | Set-ScheduledJob -FilePath C:\\Scripts\\Get-FullInventory.ps1 -Passthru
刚刚发布的PowerShell中进行了更新,包括Unregister-ScheduledJob
所看到这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.