在任务计划中添加第二个触发器 [英] Adding a second trigger in task schedule

查看:604
本文介绍了在任务计划中添加第二个触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有一个Powershell脚本,该脚本将创建计划任务,以通过Octopus部署到Windows 2012服务器.

Have a powershell script that will create a scheduled task, for deployment, by way of Octopus, to a Windows 2012 server.

Function Create-ScheduledTask($TaskName,$RunAsUser,$TaskRun,$Schedule,$StartTime,$StartDate,$Arguments,$RunWithElevatedPermissions,$Days,$Password){
    # set up
    $Command = "schtasks.exe /create /ru `"$RunAsUser`" /tn `"$TaskName`" /tr `"'$($TaskRun)' $Arguments`" $cmdSchedule $cmdDays $cmdStartDate $cmdStartTime /F $cmdInterval $cmdDuration $cmdRunLevel $cmdPassword"

    echo $Command
    Invoke-Expression $Command            
 }

在命令行上尝试将另一个触发器添加为同一任务名称的一部分将无法与schtasks.exe一起使用,这似乎与可以完成此操作的GUI相矛盾.

Attempting to add another trigger as part of the same taskname, on the command line, will not work with schtasks.exe which seemingly contradicts the GUI where it can be done.

这是用于创建事件触发器的功能,理想情况下,是将其附加到相同的计划任务上.

This is the function that was used to create the event trigger, ideally, to attach that to the same scheduled task.

Function Create-ScheduledTaskEvent($TaskName,$RunAsUser,$TaskRun,$Arguments,$RunWithElevatedPermissions,$Password, $xPath, $channelName){

    $cmdRunLevel = if(-Not $RunWithElevatedPermissions){""}else{"/rl HIGHEST"}
    $cmdPassword = if([string]::IsNullOrEmpty($Password)){""}else{"/rp `"$Password`""}
    $cmdXPath = if([string]::IsNullOrEmpty($xPath)){""}else{"/sc ONEVENT /MO `"$xPath`" "}
    $cmdRunLevel = if(-Not $RunWithElevatedPermissions){""}else{"/rl HIGHEST"}

    $Command = "schtasks.exe /create $cmdRunLevel /ru `"$RunAsUser`" $cmdXPath /tn `"$TaskName`" /tr `"'$($TaskRun)' $Arguments`" /ec `"$channelName`" "

    echo $Command          
    Invoke-Expression $Command            
 }

问题是,用/change替换开关/create仅会破坏先前计划的任务的触发器/动作.

Issue is, replacing the switch /create with /change only ends up clobbering the previous scheduled task's trigger/action.

任何想法都可以通过命令行上的schtasks.exe来完成,以将触发器组合为一个.

Any idea how can this be done by way of schtasks.exe on the command-line, to combine the triggers into one.

这可以通过使用不同的任务名称创建一个单独的任务计划来完成,但这并不理想,也不是将任务导出为xml然后再导入回来.

It could be done by creating a separate task schedule with different taskname, it is not ideal though, nor is exporting out task as xml and then importing back in.

推荐答案

schtasks.exe不允许您通过基本开关执行此操作.为了能够使用schtasks.exe执行此操作,可以使用现有任务的XML导入如SU 所述.

schtasks.exe won't allow you to do this via basic switches. In order to be able to do this with schtasks.exe you can use an XML import, of an existing task, as mentioned on SU.

导出具有所需触发器集的现有任务,以便以后导入. PowerShell也可以通过[xml]强制转换和Select-XML本机理解xml文件,因此您不仅限于静态XML文件,还可以根据需要即时更改现有文件.

Take an export of an existing task with the trigger set you want that you can import later. PowerShell can also natively understand xml files via the [xml] cast and Select-XML so you are not limited to static XML files but can also make changes on the fly to existing ones if you wanted.

根据您正在使用的系统 ,您还可以访问PowerShell任务操作cmdlet,例如 TechTarget >

Depending on what system you are working from you can also have access to the PowerShell task manipulation cmdlets like Register-ScheduledTask which does allow multiple tasks to be configured. These can be found on Windows Server 2012 R2 and Windows 8.1. To pull a small snippet from TechTarget

$triggers = @()
$triggers += New-ScheduledTaskTrigger -Daily -At 03:00
$triggers += New-ScheduledTaskTrigger -Daily -At 09:00
$triggers += New-ScheduledTaskTrigger -Daily -At 15:00
$triggers += New-ScheduledTaskTrigger -Daily -At 21:00

#..... code truncated to only show trigger portion

$action = New-ScheduledTaskAction -Execute $pstart -Argument $actionscript
Register-ScheduledTask -TaskName $taskname -Action $action -Trigger $triggers -RunLevel Highest -Description "Test job"

自从您标记了2012年以来,如果不是8.1版本,您也许可以在自己的系统中的PSSession中运行这些cmdlet

Since you tagged 2012 you might be able to run those cmdlets in a PSSession from your own system if it is not 8.1

这篇关于在任务计划中添加第二个触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆