所有服务启动后在 Windows 2012 R2 中安排任务

所有服务启动后在 Windows 2012 R2 中安排任务

我们有一台 Windows 2012 R2,其中某些服务由于依赖于其他服务而配置为延迟启动,但未显示在相应的选项卡中。如果这些服务未按此方式配置,则无法启动。

由于服务器仅在工作时间启动,我们希望在所有服务启动并运行时发送邮件,但任务计划程序中不存在这样的触发器。

一项任务被安排在“系统启动时”但显然它不能按预期进行。

如何实现?有没有办法添加对服务的依赖?

答案1

虽然不是真正需要的,但在系统启动时运行此脚本就可以了

' VB Script Document
option explicit
Const MAIL_SERVICE = "mail_service"
Const VCS_SERVICE  = "our_vcs_service"

Dim args,status,objShell
Set args = Wscript.Arguments
' esperar el correo
status = wait_service (MAIL_SERVICE,10,6)
If (status = 0) Then
 ' esperar el control de versiones
 status = wait_service (VCS_SERVICE,10,6)
 Do While (status <> 0)
   status = wait_service (VCS_SERVICE,10,6) 
 Loop
End If

If (status = 0) Then
 Set objShell = CreateObject("WScript.Shell") 
 objShell.Run "sendEMail.exe ..." 
End If


Function wait_service (service_name,seconds,retries)
 Dim svcs 
 
 seconds = 1000*seconds

 ' no funciona
 wait_service = 1
 svcs = service_status (service_name)
 If (svcs = 1) Then
   Exit Function
 End If 
 
 ' iterar, esperando los segundos indicados 
 Do While (svcs <> 0) And (retries <> 0)
   retries = retries - 1
   WScript.Sleep seconds
   svcs = service_status (service_name) 
 Loop
 
 wait_service = svcs 
End Function

 
Function service_status (service_name)
 Dim wmi
 Dim svcs,svc
 ' no existe
 service_status = 1
 
 Set wmi = GetObject("winmgmts:\\.\root\cimv2")
 Set svcs = wmi.ExecQuery("Select * from Win32_Service where Name = '" & service_name & "'")
 If (svcs.Count = 0) Then
   Exit Function
 Else  
   ' no está ejecutando
   service_status = 2
   ' https://stackoverflow.com/questions/2378723/get-first-record-from-wmi-execquery'
   Set svc = svcs.ItemIndex(0)
   If (svc.State = "Running") Then
     service_status = 0
   End If
 End If
End Function  

该脚本等待邮件服务器启动并运行,然后等待 VCS 服务器。如果两者都在运行,则发送邮件

相关内容