简体   繁体   English

为 Powershell 中的计划任务创建每月触发器(带有附加条件)

[英]Create monthly trigger for Scheduled Task in Powershell (With additional criteria)

I'm currently working on a script that when run, creates some Scheduled tasks that makes the host machine do several things and then restart within a specified time span.我目前正在编写一个脚本,该脚本在运行时会创建一些计划任务,使主机做几件事,然后在指定的时间范围内重新启动。 This script needs to be run on multiple domain controllers, and therefor i would like to "load balance" by using something like New-ScheduledTaskTrigger -RandomDelay in order for them to not reboot all at once, but kind of spread it out.该脚本需要在多个域控制器上运行,因此我想通过使用类似 New-ScheduledTaskTrigger -RandomDelay 的东西来“负载平衡”,以便它们不会一次重新启动,而是将其分散开来。

The goal is to be able to change some variables of when to restart, things like:目标是能够更改何时重新启动的一些变量,例如:

  1. First Monday of the month between 18:00 and 23:59每月第一个星期一 18:00 至 23:59
  2. Every Thursday between 01:00 and 06:00每个星期四 01:00 和 06:00 之间
  3. Every day between 04:00 and..... you see where I'm going每天 04:00 到.....你看我要去哪里

However there is no such thing as a "-Monthly" in New-ScheduledTaskTrigger但是,在 New-ScheduledTaskTrigger 中没有“-Monthly”之类的东西

That's the first problem, this one i can probably solve with the help from other posts, but if i do it for example like this I'm not able to use the -RandomDelay which I think is a major feature for this to work.这是第一个问题,我可能可以在其他帖子的帮助下解决这个问题,但如果我这样做,例如我无法使用 -RandomDelay,我认为这是它的主要功能。

Here is how I imagine it should look if the -Monthly did work (for a monthly trigger):如果 -Monthly 确实有效(对于每月触发),我想象它应该是这样的:

$rebootFrequency = MONTHLY # DAILY, WEEKLY, MONTHLY
$rebootWeek = FIRST        # FIRST, SECOND, THIRD, FOURTH, LAST
$rebootDayOfWeek = MON     # MON, TUE, WED, THU, FRI, SAT, SUN
$rebootTimeFrom = 10:00    # HH:MM[:SS]
$rebootTimeTo = 16:00      # HH:MM[:SS]


New-ScheduledTaskTrigger -"$rebootFrequency" -WeekOfMonth $rebootWeek;
-DayOfWeek $rebootDayOfWeek -At $rebootTimeFrom -RandomDelay $rebootTimeTo

Do you have any suggestions as to how I should solve this problem?你对我应该如何解决这个问题有什么建议吗?

I could do the same thing with schtask.exe, however I would end up having to make some kind of script to do the "RandomDelay" function.我可以用 schtask.exe 做同样的事情,但是我最终不得不制作某种脚本来执行“RandomDelay”function。

Feel free to ask further if you have any questions.如果您有任何问题,请随时进一步询问。 Thanks in advance.提前致谢。

Challenge 1挑战一

I've now got it to work, but I'm trying to make the script a bit more intuitive, but I can't figure out how i would do it...我现在已经让它工作了,但我试图让脚本更直观一点,但我不知道我会怎么做......

What i want to do is to "convert" from using the numbers in days (for example: 16 for Thursday) to being able to write "THU" instead.我想要做的是将使用天数(例如:星期四 16)“转换”为能够写“THU”。

Right now it looks something like this:现在它看起来像这样:

$rebootDaysOfWeek = "16" # SUN=1, MON=2, TUE=4, WED=8, THU=16 etc.
$trigger.DaysOfWeek = $rebootDaysOfWeek

But I would find it alot cooler if it was something like this:但如果是这样的话,我会发现它更酷:

$rebootDaysOfWeek = "THU" # SUN, MON, TUE, WED, THU, FRI, SAT
$trigger.DaysOfWeek = $rebootDaysOfWeek

But I can't seem to find a way to "convert" $rebootDaysOfWeek to work with the bit mask.但我似乎找不到“转换” $rebootDaysOfWeek 以使用位掩码的方法。

Check out the Microsoft Docs: https://docs.microsoft.com/en-us/windows/win32/taskschd/time-trigger-example--scripting-查看 Microsoft 文档: https://docs.microsoft.com/en-us/windows/win32/taskschd/time-trigger-example--scripting-

The sample is in VB, but it looks like it's just a ComObject.该示例在 VB 中,但看起来它只是一个 ComObject。 I haven't had enough time to play around, but you can start like this:我没有足够的时间玩,但你可以这样开始:

$service = new-object -comobject Schedule.Service
$service.connect()
$taskdefinitiion = $service.NewTask(0)

There's lots of task definition stuff, but it get's down to the triggers and you'll do this:有很多任务定义的东西,但它归结为触发器,你会这样做:

$triggers = $taskDefinition.Triggers
$trigger = triggers.Create(5) # I had to try different numbers here, didn't dig through the docs
$trigger.DaysOfWeek = 16 #Thursday
$trigger.WeeksOfMonth = 1 # First week, 2 for second, 6 for third, 8 for forth
$trigger.MonthsOfYear = 4095 # all months
$trigger.RandomDelay = 'PT1H' # 1 hour random delay.

I'll let you take it from here.我会让你从这里拿走。 Links to some of the items above: https://docs.microsoft.com/en-us/windows/win32/taskschd/monthlydowtrigger-daysofweek https://docs.microsoft.com/en-us/windows/win32/taskschd/monthlydowtrigger-monthsofyear https://docs.microsoft.com/en-us/windows/win32/taskschd/monthlydowtrigger-weeksofmonth https://docs.microsoft.com/en-us/windows/win32/taskschd/monthlydowtrigger-randomdelay上述部分项目的链接: https: //docs.microsoft.com/en-us/windows/win32/taskschd/monthlydowtrigger-daysofweek https: //docs.microsoft.com/en-us/windows/win32/taskschd/ /monthlydowtrigger-monthsofyear https: //docs.microsoft.com/en-us/windows/win32/taskschd/monthlydowtrigger-weeksofmonth https: //docs.microsoft.com/en-us/windowstrigger-randomdelay3

UPDATE FOR CHALLENGE 1挑战 1 更新

In order to use "friendly" references to the bitwise decimal value you can either create a constants section or use hashtable, either way you are going to have to do the conversion yourself:为了使用对位十进制值的“友好”引用,您可以创建一个常量部分或使用哈希表,无论哪种方式您都必须自己进行转换:

# Constants
$SUN = 1
$MON = 2
$TUE = 4
$WED = 8
$THU = 16
$FRI = 32
$SAT = 64

# Hashtable - because why not!
$DaysOfWeek = @{
  SUN = 1
  MON = 2
  TUE = 4
  WED = 8
  THU = 16
  FRI = 32
  SAT = 64
}

Then you can use: $trigger.DaysOfWeek = $THU or $trigger.DaysOfWeek = $DaysOfWeek["THU"]然后你可以使用: $trigger.DaysOfWeek = $THU$trigger.DaysOfWeek = $DaysOfWeek["THU"]

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

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