简体   繁体   English

Powershell-每月计划任务触发器

[英]Powershell - Monthly Scheduled Task Trigger

I'm currently automating the creation of scheduled tasks via Powershell, and I'm using the New-ScheduledTaskAction , New-ScheduledTaskTrigger , and Register-ScheduledTask commands. 我目前正在通过Powershell自动执行计划任务的创建,并且正在使用New-ScheduledTaskActionNew-ScheduledTaskTriggerRegister-ScheduledTask命令。 Now, I have a few jobs that need to run under the following conditions : 现在,我有一些工作需要在以下条件下运行:

  • Run once every Friday 每个星期五运行一次
  • Run once on the 1st of every month 每月1号运行一次
  • Run once on the 10th day of every month 每个月的第10天运行一次

While the technet documentation for the New-ScheduledTaskTrigger command specifies a Daily and Weekly time span parameter, I do not see one for Monthly , which is critical for defining the run times above. 尽管New-ScheduledTaskTrigger命令的technet文档指定了DailyWeekly时间跨度参数,但我看不到Monthly一个,这对于定义上述运行时间至关重要。

In the few years I've been using Powershell, I can't think of an instance where I could do something via the standard UI interface, that I couldn't accomplish using one of the available commands. 在使用Powershell的几年中,我无法想到可以通过标准UI界面执行某些操作的实例,而我无法使用其中一个可用命令来完成该任务。

Is this just flat out not available here, or am I missing something? 这只是在这里不可用,还是我缺少了什么?

UPDATE #1 更新#1

I just stumbled upon this SuperUser question , which does look promising, but references PSV3 instead of PSV4 - going to give it a shot and report back. 我只是偶然发现了这个SuperUser问题 ,看起来确实很有前途,但是引用了PSV3而不是PSV4-会给它一个机会并进行报告。

As I said in the original post, the SuperUser question above looked promising, but ultimately did not work with PSV4, and the example given in the post was basically a copy\\paste job with almost no context. 正如我在原始帖子中所说,上面的SuperUser问题看起来很有希望,但最终不适用于PSV4,并且帖子中给出的示例基本上是几乎没有上下文的复制/粘贴作业。

I realized I could leverage Schtasks.exe from my Powershell script to handle the monthly aggregations, and it's fairly easy to set up, albeit somewhat tedious : 我意识到我可以利用Powershell脚本中的Schtasks.exe来处理每月的汇总,并且设置起来相当容易,尽管有些乏味:

# set the trigger depending on time, span, and day
$runSpan = $task.SpanToRun;

if ($runSpan.Equals("Daily"))
{            
    $trigger = New-ScheduledTaskTrigger -Daily -At $task.TimeToRun
    Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $task.TaskName -User $Username -Password $Password -Description $task.Description       
}

if ($runSpan.Equals("Weekly"))
{            
    $trigger = New-ScheduledTaskTrigger -Weekly -At $task.TimeToRun -DaysOfWeek $task.DayToRun
    Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $task.TaskName -User $Username -Password $Password -Description $task.Description
}        

#  script out SchTasks magic to create the monthly tasks
if ($runSpan.Equals("Monthly"))
{
    $taskParams = @("/Create",
                "/TN", $task.TaskName, 
                "/SC", "monthly", 
                "/D", $task.DayToRun, 
                "/ST", $task.TimeToRun, 
                "/TR", $filePath, 
                "/F", #force
                "/RU", $Username,
                "/RP", $Password);

    # supply the command arguments and execute  
    #schtasks.exe $taskParams
     schtasks.exe @taskParams
}

I'm using an in-script class to keep track of all the task properties ( $task.TimeToRun , $task.DayToRun , etc.), iterating over a list of those, applying the Powershell implementation for daily and weekly tasks, then switching to SchTasks.exe for the monthly spans. 我正在使用一个脚本内类来跟踪所有任务属性( $task.TimeToRun$task.DayToRun等),遍历所有任务的属性,将Powershell实现应用于每日和每周任务,然后切换到SchTasks.exe获取每月跨度。

One thing I want to note, is that at first glance, I thought setting the user context under which the task runs could be achieved with the U and P arguments, but that is not the case. 我要注意的一件事是,乍一看,我认为可以使用UP参数来设置运行任务的用户上下文,但事实并非如此。 That specifies the creds that Schtasks.exe runs under - in order to set the user context for the task, you must use RU and RP . 这指定了Schtasks.exe运行的凭据-为了设置任务的用户上下文,必须使用RURP

In addition to the link above, these two were also very helpful : 除了上面的链接,这两个也非常有帮助:

http://coding.pstodulka.com/2015/08/02/programmatically-scheduling-powershell-scripts-in-task-scheduler/ http://coding.pstodulka.com/2015/08/02/programmatically-scheduling-powershell-scripts-in-task-scheduler/

https://msdn.microsoft.com/en-us/library/windows/desktop/bb736357(v=vs.85).aspx https://msdn.microsoft.com/zh-CN/library/windows/desktop/bb736357(v=vs.85).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM