VBScript启动多个服务

时间:2014-01-10 04:59:26

标签: vbscript

我需要使用VBScript启动多个服务。 我写了这个脚本来启动服务,当我运行它时,它不会给出任何错误,也不会启动服务。问题可能是什么想法?

    sComputer = "."
    aTargetSvcs= Array ("ServiceOne" &_
    "ServiceTwo" &_
    "ServiceThree" &_
    "ServiceFour")
     Set oWMIService = GetObject("winmgmts:" & "{impersonationlevel=impersonate}!\\" _
     & sComputer & "\root\cimv2")
     Set cServices = oWMIService.ExecQuery("SELECT * FROM Win32_Service")
     For Each oService In cServices
     For Each sTargetSvc In aTargetSvcs
     If LCase(oService.Name) = LCase(sTargetSvc) Then
     If oService.State = "Stopped" Then
     oService.StartService()
    End If
    End If
    Next
    Next

2 个答案:

答案 0 :(得分:0)

你的aTargetSvcs不是你想象的那样 - 一个包含四个字符串的数组 - 但是:

Option Explicit

Dim aTargetSvcs : aTargetSvcs = Array ("ServiceOne" &_
    "ServiceTwo" &_
    "ServiceThree" &_
    "ServiceFour")
WScript.Echo UBound(aTargetSvcs)
WScript.Echo aTargetSvcs(UBound(aTargetSvcs))

输出:

cscript 21036510.vbs
0
ServiceOneServiceTwoServiceThreeServiceFour

答案 1 :(得分:0)

我终于开始工作了。我不得不使用逗号分隔。这里的变化和它的工作原理: 但是,如何让每个服务都很容易,因为我有很多服务可以启动。如果有办法单独制作每项服务,那么阅读和管理就会容易得多。

sComputer = "."
aTargetSvcs= Array ("ServiceOne","ServiceTwo","ServiceThree")
Set oWMIService = GetObject("winmgmts:" & "{impersonationlevel=impersonate}!\\" _
  & sComputer & "\root\cimv2")
Set cServices = oWMIService.ExecQuery("SELECT * FROM Win32_Service")
For Each oService In cServices
 For Each sTargetSvc In aTargetSvcs
  If LCase(oService.Name) = LCase(sTargetSvc) Then

    If oService.State <> "Stopped" Then
      oService.StartService()    
     End If
End if
Next
Next