IIS Monitoring--Automatic Restart Scripting tool

Source: Internet
Author: User
Tags system log

Function: IIS can be restarted automatically when IIS terminates unexpectedly, no need to login system, no duty
Conditions:
1. The server has installed Windows Script 5.6 to run the. vbs script correctly.
2. Ensure that the system service Windows Management Instrumentation normal boot.

Copy the following code to Fu.vbs, put it under the system disk winnt/system32/, and note that when renaming and "Cscript//h:cscript&&cscript Fu.vbs" The fu.vbs of the scheduled task startup times cannot find a file error.
Then do a scheduled task to run the monitor when the system starts
VBS CODE:

Strfullname = Wscript.fullname
Strwshhost = Right (Strfullname, 11)
' WScript.Echo ' Default Script Host: & Strwshhost
If strwshhost = "WScript.exe" Then
Set Objshell = CreateObject ("Wscript.Shell")
Objshell.run _
"%comspec%/k" "cscript//h:cscript&&cscript Fu.vbs" ", _
Maximize_window
If err.number <> 0 Then
WScript.Echo "Error 0x" & Hex (Err.Number) & "occurred." & _
Err.Description & "." & VbCrLf & _
"Could not temporarily change the default script Host to Cscript."
Err.Clear
Wscript.Quit
End If
Wscript.Quit
End If
'----The above code is forced to the Cscript command line, generally in the cmd command line to enter Cscript *.vbs, if the direct execution of the VBS will be opened with WScript, so will only play a dialog box, I do not like, do not want to cscript every time, So to write a little extra code, double click will call a WScript execution Csript, and then destroy themselves, fortunately Microsoft's website provides this code------------------------------------
StrComputer = "."
Set objWMIService = GetObject ("winmgmts:" _
& "{impersonationlevel= impersonate, (Security)}!//" & StrComputer & "/root/cimv2")
Set Colmonitoredevents = Objwmiservice.execnotificationquery _
("SELECT * FROM __InstanceCreationEvent where TargetInstance Isa ' win32_ntlogevent ' and targetinstance.eventcode = ' 7031 ' ")
WScript.Echo "Start monitoring IIS in" &now
Todo
Set objlatestevent = colmonitoredevents.nextevent
WScript.Echo "Restart IIS" &now
Set Objshell = CreateObject ("Wscript.Shell")
Objshell.run "%comspec%/C IISRESET. EXE ", 1
'------Core Procedures

Whichfile= ("iisresetlog.htm")
Set Fso=createobject ("Scripting.FileSystemObject")
If not FSO. FileExists (Whichfile) Then
Iomode=2:create=true
' Set MyFile = fso. CreateTextFile (Whichfile,true)
Else
Iomode=8:create=false
' Set MyFile = fso. CreateTextFile (Whichfile)
End If
Set MyFile = FSO. OpenTextFile (Whichfile,iomode,create)
' MyFile.WriteLine (event occurred in ' &now& Restart IIS succeeded ' &vbctrf)
If Create=true Then
MyFile.WriteLine ""
MyFile.WriteLine ""
MyFile.WriteLine ""
MyFile.WriteLine ""
MyFile.WriteLine ""
MyFile.WriteLine ""
End If
MyFile.WriteLine "IIS Restart Time: <font color=red>" &now& "</font>"
"MyFile.WriteLine" Success: <> "
MyFile.WriteLine "<br>"
MyFile.WriteLine ""
Myfile.close
Set fso=nothing
Loop
'-----Use FSO to log IIS restart logs
Note: Targetinstance.eventcode = ' 7031 ' This code controls under what circumstances the reboot iis,7031 is the system's event ID code, that is, when the system log has 7031 such events, restart IIS.
The following is a description of the system 7031 event: The IIS Admin service was terminated unexpectedly, which has occurred 1 times. The following corrective action will run in 1 milliseconds: Run the configured recovery program.
Similarly, you can trigger this script based on a different event ID, modifying Objshell.run "%comspec%/C IISRESET." EXE ", 1 This line of code, you can perform any program supported by Win32, that is, can trigger the execution of arbitrary programs based on any event, which is more widely used.

Other:
%comspec% opens a command line window. %comspec% is an environment variable that points to the current command-line shell. By using%comspec%, you do not have to worry about whether the command line shell is cmd.exe or command.exe;%comspec% automatically chooses the correct one.

/k  after you invoke the  Dir  command, make sure that the window remains open. This is the use of  /k  parameters. If we want to make sure that the command window is closed automatically after the  Dir  command call completes, you should modify  /k  (keep)   to  /c  (close).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.