标题: qtp check windows service state [打印本页] 作者: li821022 时间: 2009-6-4 13:34 标题: qtp check windows service state QTP 怎样实现获得某个服务是否存在,并检查当前状态
eg:check apache service if exist and check its states作者: xiaoyaoke 时间: 2009-6-4 16:09
检查本机的还是远端主机的?
本地主机的wmi可以,获取running service列表,然后看看服务在不在
直接用下面的函数,没有任何返回值,为什么呐?
Private Function IsServiceRunning(strServiceName)
Dim strComputer, objWMIService, colRunningServices, objService
' Connect to local host
strComputer = "."
objService="Apache2"
' Connect to WMI interface on computer
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
' Select the specified service
Set colRunningServices = objWMIService.ExecQuery("Select * from Win32_Service where Name='" & strServiceName & "'")
' Read the services state
For Each objService in colRunningServices
Wscript.Echo objService.DisplayName & VbTab & objService.State
Call PrintLn("<strong>" & objService.DisplayName & ": " & objService.State & "</strong>")
If objService.State = "Running" Then
IsServiceRunning = True
Else
IsServiceRunning = False
End If
Next
' Clean up
Set objWMIService = Nothing
Set colRunningServices = Nothing
Set objService = Nothing
End Function作者: xiaoyaoke 时间: 2009-6-4 20:35
Function IsServiceRunning(strServiceName)
IsServiceRunning = False
Set wmi=GetObject("winmgmts:\\.")
Set pro_s=wmi.instancesof("win32_Service")
For Each p In pro_s
If p.name=strServiceName and p.started = True Then
IsServiceRunning = True
End If
Next
Set pro_s=Nothing
Set wmi =Nothing
End Function