ITPub博客

首页 > Linux操作系统 > Linux操作系统 > wmi实现iis的智能重启(转)

wmi实现iis的智能重启(转)

原创 Linux操作系统 作者:jcszjswkzhou 时间:2019-06-16 07:27:05 0 删除 编辑
公司的web服务器老是由于dllhost.exe 的内存占用过大而当掉,导致asp页面不能正常访问,最可气的是这种情况每每发生在我休假的时候,不得已被拉回来重启一下iis,为了使自己的五一长假能安稳些,写了个iis的自动重启程序,我见网上类似的程序,都是定时重启,我理想的状况是不要过于频繁启动,只在dllhost.exe这个进程当掉的时候,再重启,于是有了下面这段代码,原理是监视应用程序日志,当出现事件id是5的错误时(无法为asp程序提供足够内存),重启iis,呵呵。

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
'----上面这段代码是强制到cscript命令行,一般要在cmd命令行下输入cscript *.vbs,如果直接执行vbs会用wscript打开,这样只会弹个对话框,我不喜欢,有不想每次都cscript,所以要写点额外的代码,双击后会调用一个wscript执行csript,然后再消灭自己,幸好微软的网站上提供了这个代码------------------------------------
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel= impersonate, (Security)}!\" & strComputer & " ootcimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("Select * from __instancecreationevent where TargetInstance isa'Win32_NTLogEvent' and TargetInstance.EventCode = '4' ")
Wscript.Echo "开始监视iis于 "&now
Do
Set objLatestEvent = colMonitoredEvents.NextEvent
Wscript.Echo "重新启动iis "&now
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "%COMSPEC% /c IISRESET.EXE",,1
'------核心程序,呵呵,短吧,wmi万岁

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("事件发生于 "&now&" 重启iis成功"&vbctrf)
if create=true then
MyFile.WriteLine ""
MyFile.WriteLine ""
MyFile.WriteLine "Process Report"
MyFile.WriteLine ""
MyFile.WriteLine ""
MyFile.WriteLine "

iisreset Report

" & vbCrLf
end if
MyFile.WriteLine ""
MyFile.WriteLine ""
MyFile.WriteLine _
""
MyFile.WriteLine ""
MyFile.WriteLine ""
MyFile.WriteLine "
iis重启时间:" &now& "是否成功:
"
MyFile.Close
set fso=nothing
Loop
'-----用fso记录iis 重启日志。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10294527/viewspace-124047/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2007-08-29

  • 博文量
    3488
  • 访问量
    2602443