2008-09-11 93 views
5

我想使用任務計劃程序或類似工具來遠程重啓服務器並等待服務器重新啓動,從而自動執行Windows 2000+服務器重啓過程。我可以發出shutdownpsshutdown來遠程重新啓動,但我希望比sleep更好的等待它回來。我需要確認它在n分鐘內恢復在線或發生錯誤。通過'回到線上',我想驗證更多的不僅僅是它可以ping通,但也許它的RFC服務正在響應或一些其他確定的重要標誌。如何在重啓後檢測Windows服務器是否可用?

我更喜歡NT腳本的方法,但我不排除編寫自定義工具來做到這一點。

任何想法?

回答

2

您的遠程重啓腳本可能啓動服務器,等待n分鐘,然後查詢您的RFC服務。你也可以在服務器上有一個本地腳本來做同樣的事情。

2

您可以使用psservice來查詢RFC或後臺打印程序服務的狀態。後臺打印程序通常是最後一個啓動的服務之一。您可以使用如下語法:

psservice \\someothermachine query spooler 

一旦服務運行,就會返回類似這樣的內容。

 
SERVICE_NAME: Spooler                    
DISPLAY_NAME: Print Spooler                  
Manages all local and network print queues and controls all printing jobs. If this service is stop 
ped, printing on the local machine will be unavailable. If this service is disabled, any services 
that explicitly depend on it will fail to start.             
     GROUP    : SpoolerGroup               
     TYPE    : 110 WIN32_OWN_PROCESS INTERACTIVE_PROCESS        
     STATE    : 4 RUNNING                
           (STOPPABLE,NOT_PAUSABLE,ACCEPTS_SHUTDOWN)       
     WIN32_EXIT_CODE : 0 (0x0)                
     SERVICE_EXIT_CODE : 0 (0x0)                
     CHECKPOINT  : 0x0                 
     WAIT_HINT   : 0x0                

如果其他機器沒有準備好,你就會得到這樣

Unable to connect to \\someothermachine:                 
The RPC server is unavailable.
0

您可以查詢一些核心服務,看它是否已經啓動:

sc "\\server_name" query EventSystem 
0

使用nmap獲取機器上的開放服務列表並解析結果以確保您需要的是活動的。確保不需要需要的是而不是有效。

0

這裏的關鍵是我需要編寫腳本。是否有更簡潔的方式從psservice/sc query中提取服務狀態?我可以將它輸入到findstr "RUNNING",但是有一個更好的方法。

8

這方面的工作了一段時間後,我想出了下面的VBScript。隨意評論/改進。

' 
' Remotely reboot a server and 
' wait for server to come back up. 
' 
' Usage: cscript /nologo /E:VBScript RebootWait.vbs <Server Name> 
' 
' Shawn Poulson, 2008.09.11 
' 

' 
' Get server name from command line 
' 
If WScript.Arguments.Count <> 1 Then 
    ShowUsage() 
    WScript.Quit(1) 
End If 

ServerName = WScript.Arguments(0) 

' 
' Verify server is currently up 
' 
WScript.StdOut.WriteLine Now & ": Verify server '" & ServerName & "' is currently up..." 
If Not IsAvailable(ServerName) Then 
    WScript.StdOut.WriteLine "Error: Server is down. Reboot aborted!" 
    WScript.Quit(1) 
End If 
WScript.StdOut.WriteLine Now & ": Server is up." 

' 
' Reboot server 
' 
WScript.StdOut.WriteLine Now & ": Rebooting server '" & ServerName & "'..." 
RebootStatus = RebootServer(ServerName) 
If RebootStatus < 0 Then 
    WScript.StdOut.WriteLine "Error: Reboot returned error " & RebootStatus 
    WScript.Quit(1) 
End If 
WScript.StdOut.WriteLine Now & ": Reboot command was successful" 

' 
' Wait for server to come down 
' 
WScript.StdOut.Write Now & ": Waiting for server '" & ServerName & "' to go down..." 
WaitCount = 0 
Do While IsAvailable(ServerName) 
    WaitCount = WaitCount + 1 
    If WaitCount > 60 Then ' 5 min timeout 
     WScript.StdOut.WriteLine "Error: Timeout waiting for server to come down!" 
     WScript.Quit(1) 
    End If 
    WScript.StdOut.Write(".") 
    WScript.Sleep(5000) 
Loop 
WScript.StdOut.WriteLine "Success!" 
WScript.StdOut.WriteLine Now & ": Server is down." 

' 
' Wait for server to come back up 
' 
WScript.StdOut.Write Now & ": Waiting for server '" & ServerName & "' to come back up..." 
WaitCount = 0 
Do While Not IsAvailable(ServerName) 
    WaitCount = WaitCount + 1 
    If WaitCount > 240 Then ' 20 min timeout 
     WScript.StdOut.WriteLine "Error: Timeout waiting for server to come back up!" 
     WScript.Quit(1) 
    End If 
    WScript.StdOut.Write(".") 
    WScript.Sleep(5000) 
Loop 
WScript.StdOut.WriteLine "Success!" 
WScript.StdOut.WriteLine Now & ": Server is back up after reboot." 

' 
' Success! 
' 
WScript.Quit(0) 


Sub ShowUsage() 
    WScript.Echo "Usage: " & WScript.ScriptName & " <Server name>" 
End Sub 

' Returns: 
' 1 = Successfully issued reboot command 
' -2 = Could not reach server 
' -3 = Reboot command failed 
Function RebootServer(ServerName) 
    Dim OpSystem 
    On Error Resume Next 
    For Each OpSystem in GetObject("winmgmts:{(Shutdown)}!\\" & ServerName & "\root\CIMV2").ExecQuery("select * from Win32_OperatingSystem where Primary=true") 
     On Error GoTo 0 

     If IsObject(OpSystem) Then 
     ' Invoke forced reboot 
     If OpSystem.Win32Shutdown(6, 0) = 0 Then 
      ' Success 
      RebootServer = 1 
     Else 
      ' Command failed 
      RebootServer = -3 
     End If 

     Else 
     RebootServer = -2 

     End If 
    Next 
End Function 

' Return True if available 
Function IsAvailable(ServerName) 
    ' Use Windows RPC service state as vital sign 
    IsAvailable = (GetServiceState(ServerName, "RpcSs") = "Running") 
End Function 

' Return one of: 
' Stopped, Start Pending, Stop Pending, 
' Running, Continue Pending, Pause Pending, 
' Paused, Unknown 
Function GetServiceState(ServerName, ServiceName) 
    Dim Service 
    On Error Resume Next 
    Set Service = GetObject("winmgmts:\\" & ServerName & "\root\CIMV2:Win32_Service='" & ServiceName & "'") 
    On Error GoTo 0 
    If IsObject(Service) Then GetServiceState = Service.State 
End Function 
+0

不錯!我測試了這一點,它可以像廣告一樣工作。您需要使用在遠程服務器上擁有管理權限的帳戶運行;提供這種信用的能力會很好。 – quux 2008-11-03 19:56:34

相關問題