用 WMI 查詢一個服務是否執行, 若不是把它執行


以 SQL Server Agent 為例, 服務名稱可以自行替換

Option Explicit
Dim objWMIService, objService, strComputer,strService
strService="SQLSERVERAGENT"
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
For Each objService In objWMIService.ExecQuery("Select * from Win32_Service Where Name = '"_
&strService&"'")
If objService.State <> "Running" Then
objService.StartService
End If
Next
WScript.Quit

 

Enjoy. Jacky

Comments (0)

Skip to main content