gpt4 book ai didi

windows - 如何检测 Windows 服务器在重启后是否可用?

转载 作者:可可西里 更新时间:2023-11-01 13:28:18 25 4
gpt4 key购买 nike

我想使用任务计划程序或类似工具自动执行 Windows 2000+ 服务器重启过程,以远程重启服务器并等待它恢复。我可以发出 shutdownpsshutdown 来远程重启,但我想要比 sleep 更好的东西来等待它回来。我需要验证它是否在 n 分钟内重新联机,否则会引发错误。

通过“重新上线”,我想验证的不仅仅是它可以被 ping 通,还可能是它的 RFC 服务正在响应或其他一些确定的生命体征。

我更喜欢 NT 脚本方法,但我不排除编写自定义工具来执行此操作。

有什么想法吗?

最佳答案

经过一段时间的研究,我想到了以下 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

关于windows - 如何检测 Windows 服务器在重启后是否可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com