eblend
01-12-2012, 12:54 PM
Hey guys, this is a function from a script we use to do WSUS patching. The script works great to do what we need when there are patches to install, however, when we do not approve any patches and just want to run a script on each server, we would love for the server to reboot, regardless if any patches were installed or not.
This is the function which appears to do some WMI calls before doing a reboot. Is there a simpler way to reboot a server by making some changes to this function? I can't really follow all of the code in here to make senses of what it actually does.
Any help would be greatly appreciated.
Thanks
Function RestartAction
If silent = 0 Then objdiv.innerhtml = strStatus & "<br> Now performing restart action (" & restarttext & ")."
wscript.sleep 4000
writelog("Processing RestartAction")
'On Error GoTo 0
Dim OpSysSet, OpSys
'writelog("Computer: " & strComputer & vbcrlf & "Restart Action: " & strRestart)
'On Error Resume Next
'Call WMI query to collect parameters for reboot action
Set OpSysSet = GetObject("winmgmts:{(Shutdown)}//" & strComputer & "/root/cimv2").ExecQuery("select * from Win32_OperatingSystem"_
& " where Primary=true")
If CStr(err.number) <> 0 Then
strMsg = "There was an error while attempting to connect to " & strComputer & "." & vbcrlf & vbcrlf _
& "The actual error was: " & err.description
writelog(strMsg)
blnFatal = true
Call ErrorHandler("WMI Connect",strMsg,blnFatal)
End If
Const EWX_LOGOFF = 0
Const EWX_SHUTDOWN = 1
Const EWX_REBOOT = 2
Const EWX_FORCE = 4
Const EWX_POWEROFF = 8
'set PC to reboot
If strRestart = 1 Then
For each OpSys in OpSysSet
opSys.win32shutdown EWX_REBOOT + EWX_FORCE
Next
'set PC to shutdown
ElseIf strRestart = 2 Then
For each OpSys in OpSysSet
opSys.win32shutdown EWX_POWEROFF + EWX_FORCE
Next
'Do nothing...
ElseIf strRestart = "0" Then
End If
End Function
This is the function which appears to do some WMI calls before doing a reboot. Is there a simpler way to reboot a server by making some changes to this function? I can't really follow all of the code in here to make senses of what it actually does.
Any help would be greatly appreciated.
Thanks
Function RestartAction
If silent = 0 Then objdiv.innerhtml = strStatus & "<br> Now performing restart action (" & restarttext & ")."
wscript.sleep 4000
writelog("Processing RestartAction")
'On Error GoTo 0
Dim OpSysSet, OpSys
'writelog("Computer: " & strComputer & vbcrlf & "Restart Action: " & strRestart)
'On Error Resume Next
'Call WMI query to collect parameters for reboot action
Set OpSysSet = GetObject("winmgmts:{(Shutdown)}//" & strComputer & "/root/cimv2").ExecQuery("select * from Win32_OperatingSystem"_
& " where Primary=true")
If CStr(err.number) <> 0 Then
strMsg = "There was an error while attempting to connect to " & strComputer & "." & vbcrlf & vbcrlf _
& "The actual error was: " & err.description
writelog(strMsg)
blnFatal = true
Call ErrorHandler("WMI Connect",strMsg,blnFatal)
End If
Const EWX_LOGOFF = 0
Const EWX_SHUTDOWN = 1
Const EWX_REBOOT = 2
Const EWX_FORCE = 4
Const EWX_POWEROFF = 8
'set PC to reboot
If strRestart = 1 Then
For each OpSys in OpSysSet
opSys.win32shutdown EWX_REBOOT + EWX_FORCE
Next
'set PC to shutdown
ElseIf strRestart = 2 Then
For each OpSys in OpSysSet
opSys.win32shutdown EWX_POWEROFF + EWX_FORCE
Next
'Do nothing...
ElseIf strRestart = "0" Then
End If
End Function