¿Cómo detectar si un servidor de Windows está disponible después de un reinicio?
Pregunta
Quiero automatizar un Windows 2000+ servidor de reiniciar el proceso con el Programador de Tareas o una herramienta similar para reiniciar remotamente un servidor y esperar a que vuelva a estar arriba.Me puede emitir shutdown
o psshutdown
para reiniciar remotamente, pero yo quiero algo mejor que sleep
a esperar a que regresara.Necesito comprobar que está de vuelta en línea dentro de n
minutos o tirar un error.
Por 'en línea', me gustaría verificar más que sólo eso puede ser ping, pero tal vez su RFC servicio responde o algún otro determinado signo vital.
Prefiero un NT secuencia de comandos de enfoque, pero no voy a descartar la escritura una herramienta personalizada para hacer esto.
Alguna idea?
Solución
Su control remoto reiniciar la secuencia de comandos puede iniciar el servidor, espere a n de minutos, a continuación, consulta tu RFC servicio.Usted también podría tener un local de secuencia de comandos en el servidor de hacer la misma cosa.
Otros consejos
Después de trabajar en esto por un rato, se me ocurrió la siguiente VBScript.Siéntase libre de comentar/mejorar.
'
' 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
Usted podría utilizar psservice para consultar el estado de la RFC o Cola de Impresión servicios.La Cola es generalmente uno de los últimos servicios de empezar.Usted podría utilizar una sintaxis como:
psservice \\someothermachine query spooler
Que devolverá algo como esto una vez que el servicio se está ejecutando.
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
Si el otro equipo no está listo, vas a ver algo parecido
Unable to connect to \\someothermachine: The RPC server is unavailable.
Con VBScript (WSH) se puede comprobar con el .los bienes del estado.Este script muestra que la propiedad de ser utilizado en una aplicación diferente, pero debe ayudar a ilustrar la idea:
Uso nmap para obtener una lista de servicios abiertos en la máquina y analizar los resultados para asegurarse de que lo que necesita es activo.También es útil para asegurarse de que las cosas se no necesitamos son no activo.
Puede sondear algunos servicios básicos para ver si ha comenzado:
sc "\\server_name" query EventSystem
La clave aquí es que necesito script de este.Hay un limpiador de manera de extraer el estado de servicio de psservice
/sc query
?Soy capaz de canalizar a findstr "RUNNING"
, pero tiene que haber una mejor manera.