2014-01-10 45 views
0

我需要使用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 

回答

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 
+0

,因为当你将服务名称连接成一个字符串时,你知道如何编写多行语句(提示:_),我怀疑你正在拉腿。 –

+0

我真的很喜欢脚本和编程。经过一番研究后,我设法做到了。我会尽量使它分开。 – user3077069

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 
+0

谢谢你的帮助。我将如何解决这个问题? – user3077069

+0

@ user3077069 - 创建一个由四个元素组成的数组,而不是将(&)四个字符串连接成一个。你看过Array()函数的文档(http://msdn.microsoft.com/en-us/library/dsxhtazh%28v=vs.84%29.aspx)? –