2014-03-02 54 views
0
sComputer = "." 
Set Runninglist = CreateObject("System.Collections.ArrayList") 
aTargetSvcs= Array ("pageserver","CacheServer","CrystalAPS","CrystalInputFileServer","CrystalOutputFileServer","CrystalReportApplicationServer", "JobServer_Report") 
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 <> "Started" Then 
Runninglist.Add oService.Name 
    oService.StopService()  
End If 
End if 
Next 
Next 

WScript.Sleep 3000 

For Each oService In cServices 
For Each sTargetSvc In Runninglist 
If LCase(oService.Name) = LCase(sTargetSvc) Then 
oService.StartService() 
End If 
Next 
Next 

目標:我想在這個以開始從Runninglist服務,只有當他們先前已停止:
CrystalAPS
晶振輸入文件資源庫服務器
水晶輸出文件資源庫服務器
水晶報表應用程序服務器
水晶報表作業服務器
水晶緩存服務器
Crystal頁服務器啓動/停止服務在特定的順序只有當以前運行

+0

爲什麼不簡單定義這些服務之間的依賴關係? –

回答

0

我會嘗試利用兩個事實:

  1. 一個VBScript字典保持項的穩定秩序
  2. WMI循環對象可以存儲以備後用

在代碼:

Option Explicit 

Dim dicSrvOrd : Set dicSrvOrd = CreateObject("Scripting.Dictionary") 
dicSrvOrd.CompareMode = vbTextCompare 
Set dicSrvOrd("SQLWriter") = Nothing 
Set dicSrvOrd("Browser") = Nothing 
Set dicSrvOrd("Alerter") = Nothing 

Dim oService 
For Each oService In GetObject("winmgmts:" & "{impersonationlevel=impersonate}!\\.\root\cimv2").ExecQuery("SELECT * FROM Win32_Service") 
    If dicSrvOrd.Exists(oService.Name) Then 
     If "Running" = oService.State Then 
      WScript.Echo "stopping", oService.Name, "(was", oService.State & ")" 
      Set dicSrvOrd(oService.Name) = oService 
     Else 
      WScript.Echo oService.Name, oService.State 
     End If 
    Else 
     ' WScript.Echo "don't care for", oService.Name 
    End If 
Next 
WScript.Echo "--------------" 
Dim sSrv 
For Each sSrv In dicSrvOrd.Keys() 
    If Not dicSrvOrd(sSrv) Is Nothing Then 
     WScript.Echo "starting", dicSrvOrd(sSrv).Name 
    End If 
Next 

輸出:

cscript 22131182.vbs 
Alerter Stopped 
stopping Browser (was Running) 
stopping SQLWriter (was Running) 
-------------- 
starting SQLWriter 
starting Browser 
相關問題