vbs操作windows服务

开启
'***************************************************************************

'Set strComputer = "."

'Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")

'Set objSWbemServices = objSWbemLocator.ConnectServer _
'    (strComputer, "root\cimv2", "fabrikam\administrator", "password")
'objSWbemServices.Security_.ImpersonationLevel = 3

Set objSWbemServices = GetObject("winmgmts:{impersonationLevel=impersonate}!\.\root\CIMV2\administrator\Password")

Set ServiceSet = objSWbemServices.ExecQuery("select * from Win32_Service where Name='服务名'")

for each Service in ServiceSet
 RetVal = Service.StartService()
 if RetVal = 0 then WScript.Echo "Service started"
 if RetVal = 10 then WScript.Echo "Service already running"
next
 

停止
'***************************************************************************

'Set strComputer = "."

'Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")

'Set objSWbemServices = objSWbemLocator.ConnectServer _
'    (strComputer, "root\cimv2", "fabrikam\administrator", "password")
'objSWbemServices.Security_.ImpersonationLevel = 3

Set objSWbemServices = GetObject("winmgmts:{impersonationLevel=impersonate}!\.\root\CIMV2\administrator\Password")

Set ServiceSet = objSWbemServices.ExecQuery("select * from Win32_Service where Name='服务名'")

for each Service in ServiceSet
 RetVal = Service.StopService()
 if RetVal = 0 then 
  WScript.Echo "Service stopped" 
 elseif RetVal = 5 then 
  WScript.Echo "Service already stopped" 
 end if
next

此条目发表在technologys分类目录,贴了, 标签。将固定链接加入收藏夹。