PDA

View Full Version : Anyone good at VB script?



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

revelations
01-12-2012, 02:52 PM
Dumb question, but why not just create a batch file for a reboot command after the script ?

whoatemyling
01-14-2012, 12:53 AM
agreed, batch call the vbscript and use a psshutdown at the end of the script

eblend
01-14-2012, 09:56 AM
Originally posted by whoatemyling
agreed, batch call the vbscript and use a psshutdown at the end of the script

Yah that is an option, the only problem is, I guess I don't really know how everything works. You see, we run this on all servers during patching, it talks to the windows update service, tells it to install the patches, and after the install is done, reboot. If i made it a separate reboot via a batch of psshutdown, it would be a lot of work to figure out how to make it execute after patching only so that it doesn't reboot while the patches are being installed.

whoatemyling
01-14-2012, 10:45 PM
within batch commands you can use start /wait xxxxxxxx.exe

This forces the batch script to wait for the previous command to complete before proceeding to the next step.

In your case, you would create a simple batch script as follows:

-------------------------------------------------------
echo: "Starting VBS now"

start /wait c:\program files\vbs\xxxxxxxx1234.vbs

echo: "VBS complete, rebooting machine in 10 secs"

shutdown -r -t 10
---------------------------------------------------------


I'm assuming you're running this locally on the servers.