且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从 Powershell 更新计划任务脚本

更新时间:2022-12-02 18:33:18

好的,更改作业的文件路径很容易.首先我们得到作业,然后我们将该调度作业对象通过管道传递给 Set-ScheduledJob 并指定 -FilePath

$NewPath = GCI C:\Apps\myscript_*.ps1 |选择 -last 1 -ExpandProperty FullNameGet-ScheduledJob MyJob |Set-ScheduledJob -FilePath $NewPath

为了在信用到期时给予信用,我要感谢 Get-Help *-ScheduledJob 然后是 Get-Help Set-ScheduledJob -完整的 和示例 1 用于完成这种确切情况...

示例 1:更改作业运行的脚本第一个命令使用 Get-ScheduledJob cmdlet 来获取 Inventory预定的工作.输出显示作业运行 Get-Inventory.ps1脚本.这个命令不是必需的;它仅用于显示脚本更改的效果.PS C:\>Get-ScheduledJob -Name Inventory已启用 ID 名称触发器命令-- ---- -------- ------- -------1 库存{1} C:\Scripts\Get-Inventory.ps1 真第二个命令使用 Get-ScheduledJob cmdlet 来获取 Inventory预定的工作.管道操作符 (|) 将调度的作业发送到Set-ScheduledJob cmdlet.Set-ScheduledJob 命令使用脚本参数以指定新脚本 Get-FullInventory.ps1.命令使用 Passthru 参数在更改后返回计划的作业.PS C:\>Get-ScheduledJob -Name Inventory |Set-ScheduledJob -FilePath C:\Scripts\Get-FullInventory.ps1 -Passthru

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:

  1. 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.
  2. 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.

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

$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...

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