简体   繁体   English

Powershell在特定时间启动

[英]Powershell launch at specific times

I am building a system to restart computers for patch purposes. 我正在构建一个系统以出于修补目的重新启动计算机。 Most of the skeleton is there and working, I use workflows and some functions to allow me to capture errors and reboot the systems in a number of ways in case of failures. 大多数框架都在那里并且可以正常工作,我使用工作流和一些功能来允许我捕获错误并以多种方式在发生故障的情况下重新启动系统。

One thing I am not sure of is how to set up the timing. 我不确定的一件事是如何设置时间。 I am working on a web interface where people can schedule their reboots, either dynamic (one-time) or regularly scheduled (monthly). 我正在使用一个Web界面,人们可以在其中安排动态(一次)或定期(每月)重新启动。 The server info and times for the boots is stored in a SQL database. 引导的服务器信息和时间存储在SQL数据库中。

The part that I am missing is how to trigger the reboots when scheduled. 我缺少的部分是如何在计划时触发重新启动。 All I can think of is allowing for whole hour increments, and run a script hourly checking to see if any servers in the db have a reboot time that "matches" the current time. 我能想到的是允许整小时递增,并每小时运行一次脚本以检查数据库中是否有任何服务器的重新启动时间与当前时间“匹配”。 This will likely work, but is somewhat inflexible. 这可能会起作用,但有点不灵活。

Can anyone think of a better way? 谁能想到更好的方法? Some sort of daemon? 某种守护程序?

For instance, user X has 300 servers assigned to him. 例如,用户X分配了300台服务器。 He wants 200 rebooted at 10 PM on each Friday, and 50 once a month on Saturday at 11 PM. 他希望每个星期五晚上10点重新启动200次,每月星期六晚上11点一次重新启动200次。 There will be over a dozen users rebooting 3000-4000 computers, sometimes multiple times monthly. 将有十几个用户重新启动3000-4000台计算机,有时每月重新启动几次。

Ok, let's say you have a script that takes a date and time as an argument that will look up what computers to reboot based on that specific date and time, or schedule, or whatever it is that you're storing in your sql db that specifies how often to reboot things. 好的,假设您有一个以日期和时间为参数的脚本,该脚本将根据该特定日期和时间或计划或要存储在sql数据库中的内容来查找要重启的计算机。指定多长时间重新启动一次。 For the sake of me not really knowing sql that well, we'll pretend that this is functional (would require the PowerShell Community Extensions snapin for Invoke-AdoCommand cmdlet): 为了我不太了解sql,我们假装这是功能性的(将需要Invoke-AdoCommand cmdlet的PowerShell Community Extensions管理单元):

[cmdletbinding()]
Param([string]$RebootTime)

$ConStr = 'Data Source=SQLSrv01;Database=RebootTracking;Integrated Security=true;'
$Query = "Select * from Table1 Where Schedule = $RebootTime"
$Data = Invoke-AdoCommand -ProviderName SqlClient -ConnectionString $ConStr -CommandText $Query
$data | ForEach{Do things to shutdown $_.ServerName}

You said you already have things setup to reboot the servers so I didn't even really try there. 您说您已经设置了重新启动服务器的设置,所以我什至没有真正尝试过。 Then all you have to do is setup a scheduledjob for whenever any server is supposed to be rebooted: 然后,您要做的就是为应该重新引导任何服务器的时间设置一个Scheduledjob:

Register-ScheduledJob –FilePath \\Srv01\Scripts\RebootServers.ps1 –Trigger @{Frequency=Weekly; At="10:00PM"; DaysOfWeek="Saturday"; Interval=4} -ArgumentList @{'RebootTime'="Day=Saturday;Weeks=4;Time=22:00"}

That's an example, but you know, you could probably work with it to accomplish your needs. 那是一个例子,但是您知道,您可能可以使用它来满足您的需求。 That will run it once every 4 Saturdays (about monthly). 这将每四个星期六(大约每月一次)运行一次。 That one scheduled task will query the sql server to match up a string against a field, and really you could format that any way you want to make it as specific or general as desired for the match. 该计划任务将查询sql server,以将字符串与字段进行匹配,实际上,您可以通过任何方式设置该格式,以使其符合匹配的特定要求或常规要求。 That way one task could reboot those 200 servers, and another could reboot the other 50 all dependent on the user's desires. 这样,一项任务可以重新启动这200台服务器,而另一项任务可以重新启动其他50台服务器,这完全取决于用户的需求。

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

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