用于检查服务的脚本,并在需要时自动启动它 [英] script to check a service and autostart it if required

查看:70
本文介绍了用于检查服务的脚本,并在需要时自动启动它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我的服务进入"已停止/待定"状态模式最近几次。如何编写脚本来检查此特定服务是否处于停止/挂起模式并在需要时自动启动?使用此脚本,我将运行一个计划的服务,每15分钟运行
此脚本。请提前告知。

解决方案

Hi inenewbl


我不是专家但是这就是我掀起的 - 让我知道这对你有什么用。

 
选项 显式
Dim sComputer,sServiceName,sServiceState

'键入您要检查的计算机的名称。
'使用。对于本地机器
sComputer = "。"

"键入您要检查的服务名称。
sServiceName = " 'Apple Mobile Device'"

'Use" Running"确保其运行
'使用'停止'"确保其停止
sServiceState = "正在运行"
' sServiceState =" Stopped"

'创建一个循环以每15分钟运行一次脚本
'取消注释,如果你想使用它。
'Do
'调用子程序以检查服务的状态
ServiceCheck "。" ,sServiceName, sServiceState
'wscript.sleep(15000)
'Loop

WScript.Quit


Sub ServiceCheck(Required_Computer_Name,Required_Service_Name,Required_Service_State)
< span style ="color:Blue;"> Dim foWMIService,foItem,foService,fcolListOfServices
Dim fsComputer,fs ServiceName,fsServiceState

fsComputer = Required_Computer_Name
fsServiceName = LCase(Required_Service_Name)
fsServiceState = LCase(Required_Service_State)

" winmgmts:{impersonationLevel = impersonate}!\\" & fsComputer& " \root \ cimv2"
设置 fcolListOfServices = foWMIService。 ExecQuery(" Select * from Win32_Service Where Name =" & fsServiceName& ""

如果(fcolListOfServices。 count > 0)然后
对于 每个 foService in fcolListOfServices
'WScript.Echo foService.Name& " " &安培; foService.State
如果 (LCase(foService.state) = fsServiceState)然后
WScript.Echo ""服务状态"" &安培; foService.name& "'不是'" & fsServiceState& "'。"
如果 InStr(1,LCase(fsServiceState)) ," run" ,1)然后
StartService fsComputer,fsServiceName
结束 如果
If InStr(1,LCase(fsServiceState)," stop" ,1)然后
StopService fsComputer,fsServiceName
End 如果
其他
WScript.Echo "服务似乎是'< & fsServiceState& "'。"
End 如果
下一步
Else
WScript.Echo "错误:fscolListOfServices为空!这项服务真的存在吗?"
结束 如果
End Sub

Sub StartService(Required_Computer_Name,Required_Service_Name)
Dim foWMIService,fcolListOfServices,foService
Dim fsComputer,fsServiceName

fsComputer = Required_Computer_Name
fsServiceName = LCase(Required_Service_Name)

设置 foWMIService = GetObject(" winmgmts:{impersonationLevel = impersonate}!\\" & fsComputer& " \root \ cimv2"
设置 fcolListOfServices = foWMIService.ExecQuery( "从Win32_Service中选择*其中Name =& "& fsServiceName& " "

如果(fcolListOfServices。 count > 0)然后
对于 每个 foService in fcolListOfServices
如果 (foService.State = "" Running" 然后
WScript.Echo "正在开始服务"" & foService.Name& "'"
On 错误 恢复 下一步
foService.StartService
如果 Err.Number<> 0 然后
WScript.Echo "错误(< & Err。数字& ")启动服务'" & foService.name& "':" & Err.Description
Else
WScript.Echo "" Successfully started service"" & foService.name& "'!"
End 如果
On 错误 转到 0
结束 如果
下一步
结束 If
结束 Sub

Sub StopService(Required_Computer_Name,Required_Service_Name)
Dim foWMIService,fcolListOfServices,foService
Dim fsComputer,fsServiceName

fsComputer = Required_Computer_Name
fsServiceName = LCase(Required_Service_Name)

设置 foWMIService = GetObject(" winmgmts:{impersonationLevel = impersonate}!\\" & fsComputer& " \root \ cimv2"
设置 fcolListOfServices = foWMIService。 ExecQuery(" Select * from Win32_Service Where Name =" & fsServiceName& ""

如果(fcolListOfServices。 count > 0)然后
对于 每个 foService in fcolListOfServices
如果 (foService.State = " Stopped" 然后
WScript.Echo "停止服务"" & foService.Name& "'"
On 错误 恢复 下一步
foService.StopService
如果 Err.Number<> 0 然后
WScript.Echo "错误(< & Err。数字& ")停止服务'" & foService.name& "':" & Err.Description
Else
WScript.Echo "" Successfully stopped service"" & fsService.name& "'!"
End 如果
On 错误 转到 0
结束 如果
下一步
结束 If
结束 Sub


Hi all,

I have a service that goes into "stopped/pending" mode a few times recently. How do i write a script to check if this particular service is in stopped/pending mode and to auto start it when required? With this script i will run a scheduled service that runs this script every 15min. Pls advise, thks in advance.

解决方案

Hi inenewbl

I'm no expert but here's what I whipped up - let me know how this works for you.

Option Explicit
Dim sComputer, sServiceName, sServiceState

' Key in the name of the computer you wish to check.
' Use . for the local machine
sComputer = "."

' Key in the name of the service you wish to check on.
sServiceName = " 'Apple Mobile Device' "

' Use "Running" to ensure its running
' Use "Stopped" to ensure its stopped
sServiceState = "Running"
'sServiceState = "Stopped"

' Create a loop to run script every 15 minutes
' Uncomment if you want to use this.
'Do
	' Call Subroutine to check the status of the service
	ServiceCheck ".",sServiceName,sServiceState
'	wscript.sleep(15000)
'Loop

WScript.Quit


Sub ServiceCheck(Required_Computer_Name,Required_Service_Name,Required_Service_State)
	Dim foWMIService, foItem, foService,fcolListOfServices
	Dim fsComputer, fsServiceName, fsServiceState
	
	fsComputer = Required_Computer_Name
	fsServiceName = LCase(Required_Service_Name)
	fsServiceState = LCase(Required_Service_State)

	Set foWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & fsComputer & "\root\cimv2")
	Set fcolListOfServices = foWMIService.ExecQuery("Select * from Win32_Service Where Name =" & fsServiceName & " ")

	If (fcolListOfServices.count > 0) Then
		For Each foService in fcolListOfServices
			'WScript.Echo foService.Name & " " & foService.State
			If Not (LCase(foService.state) = fsServiceState) Then
				WScript.Echo "State of service '" & foService.name & "' is not '" & fsServiceState & "'."
				If InStr(1,LCase(fsServiceState),"run",1) Then
					StartService fsComputer,fsServiceName
				End If
				If InStr(1,LCase(fsServiceState),"stop",1) Then
					StopService fsComputer,fsServiceName
				End If
			Else
				WScript.Echo "Service appears to be '" & fsServiceState & "'."
			End If
		Next
	Else
		WScript.Echo "Error: fscolListOfServices is empty! Does this service really exist?"
	End If
End Sub

Sub StartService(Required_Computer_Name,Required_Service_Name)
	Dim foWMIService,fcolListOfServices, foService
	Dim fsComputer, fsServiceName
	
	fsComputer = Required_Computer_Name
	fsServiceName = LCase(Required_Service_Name)
	
	Set foWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & fsComputer & "\root\cimv2")
	Set fcolListOfServices = foWMIService.ExecQuery("Select * from Win32_Service Where Name =" & fsServiceName & " ")

	If (fcolListOfServices.count > 0) Then
		For Each foService in fcolListOfServices
			If Not (foService.State = "Running") Then
				WScript.Echo "Starting Service '" & foService.Name & "'"
				On Error Resume Next
				foService.StartService
				If Err.Number <> 0 Then
					WScript.Echo "Error (" & Err.Number & ") starting service '" & foService.name & "': " & Err.Description
				Else
					WScript.Echo "Successfully started service '" & foService.name & "'!"
				End If
				On Error Goto 0
			End If
		Next
	End If
End Sub

Sub StopService(Required_Computer_Name,Required_Service_Name)
	Dim foWMIService,fcolListOfServices, foService
	Dim fsComputer, fsServiceName
	
	fsComputer = Required_Computer_Name
	fsServiceName = LCase(Required_Service_Name)
	
	Set foWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & fsComputer & "\root\cimv2")
	Set fcolListOfServices = foWMIService.ExecQuery("Select * from Win32_Service Where Name =" & fsServiceName & " ")

	If (fcolListOfServices.count > 0) Then
		For Each foService in fcolListOfServices
			If Not (foService.State = "Stopped") Then
				WScript.Echo "Stopping Service '" & foService.Name & "'"
				On Error Resume Next
				foService.StopService
				If Err.Number <> 0 Then
					WScript.Echo "Error (" & Err.Number & ") stopping service '" & foService.name & "': " & Err.Description
				Else
					WScript.Echo "Successfully stopped service '" & fsService.name & "'!"
				End If
				On Error Goto 0
			End If
		Next
	End If
End Sub


这篇关于用于检查服务的脚本,并在需要时自动启动它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆