问题描述
我目前正在通过 Powershell 自动创建计划任务,并且我正在使用 New-ScheduledTaskAction
、New-ScheduledTaskTrigger
和 Register-ScheduledTask
命令.现在,我有一些作业需要在以下条件下运行:
I'm currently automating the creation of scheduled tasks via Powershell, and I'm using the New-ScheduledTaskAction
, New-ScheduledTaskTrigger
, and Register-ScheduledTask
commands. Now, I have a few jobs that need to run under the following conditions :
- 每周五运行一次
- 每月 1 号运行一次
- 每月 10 号运行一次
虽然 technet 文档对于 New-ScheduledTaskTrigger
命令指定了 Daily
和 Weekly
时间跨度参数,我没有看到 Monthly
,这对于定义上面的运行时间至关重要.
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.
在我使用 Powershell 的几年里,我想不出一个例子,我可以通过标准 UI 界面做一些事情,而我不能em> 使用可用命令之一完成.
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.
这是完全没有在这里可用,还是我遗漏了什么?
Is this just flat out not available here, or am I missing something?
更新 #1
我偶然发现了这个超级用户问题,看起来确实很有希望,但引用的是 PSV3 而不是 PSV4 - 打算试一试并报告回来.
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 问题看起来很有希望,但最终不适用于 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.
我意识到我可以利用我的 Powershell 脚本中的 Schtasks.exe 来处理每月的聚合,而且设置起来相当容易,虽然有点乏味:
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 :
# 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
}
我正在使用脚本内类来跟踪所有任务属性($task.TimeToRun
、$task.DayToRun
等),并进行迭代在这些列表中,将 Powershell 实现应用于每日和每周任务,然后切换到每月跨度的 SchTasks.exe.
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.
我要注意的一件事是,乍一看,我认为可以使用 U
和 来设置运行 task 的用户上下文>P
参数,但事实并非如此.这指定了 Schtasks.exe 在下运行的凭据 - 为了设置任务的用户上下文,您必须使用 RU
和 RP
.
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. 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
.
除了上面的链接,这两个也很有帮助:
In addition to the link above, these two were also very helpful :
https://msdn.microsoft.com/en-us/library/windows/desktop/bb736357(v=vs.85).aspx
这篇关于Powershell - 每月计划任务触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!