Quantcast
Channel: MSDN Blogs
Viewing all articles
Browse latest Browse all 5308

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

$
0
0

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

Option Explicit
Dim objWMIService, objService, strComputer,strService
strService="SQLSERVERAGENT"
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2")
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


Viewing all articles
Browse latest Browse all 5308

Trending Articles