如何编写 VBS 脚本来检查网站,然后停止和启动 Windows 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5038681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write a VBS Script to check a website and then stop and start a windows service
提问by user622723
I need to write a VBS script to check a website for certain text : "Service Unavailable" and "Error in /"
我需要编写一个 VBS 脚本来检查网站的某些文本:“服务不可用”和“错误在 /”
When it finds this I need it to restart the windows service "World wide publishing service"
当它发现这一点时,我需要它来重新启动 Windows 服务“全球发布服务”
Where do I start?
我从哪里开始?
Any help would be much appreciated!
任何帮助将非常感激!
Cheers!
干杯!
Andy
安迪
回答by user622723
You can check on a web site by using the MSXML2.XMLHTTP object (ie the same object that internet explorer uses to fire AJAX requests) and checking the status code (200 is status OK, 404 is page not found etc)
您可以通过使用 MSXML2.XMLHTTP 对象(即 Internet Explorer 用于触发 AJAX 请求的同一对象)并检查状态代码(200 是状态正常,404 是页面未找到等)来检查网站
dim http: set http = CreateObject("MSXML2.XMLHTTP")
http.open "GET", "http://site.com?param=value", false
http.send
if not http.status = 200 then
' something not right, start your service
end if
as far as starting a service is concerned, this pagehas quite a few examples of working with services, of which this is how to start one (copied verbatim, not tested):
就启动服务而言,这个页面有很多使用服务的例子,这是如何启动一个(逐字复制,未经测试):
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery _
("Select * from Win32_Service where Name='NetDDE'")
For each objService in colServiceList
errReturn = objService.StartService()
Next
Wscript.Sleep 20000
Set colServiceList = objWMIService.ExecQuery("Associators of " _
& "{Win32_Service.Name='NetDDE'} Where " _
& "AssocClass=Win32_DependentService " & "Role=Dependent" )
For each objService in colServiceList
objService.StartService()
Next
if the service you want to start is IIS, then perhaps you dispense with the http request and instead directly detect whether the IIS service is running or not
如果你要启动的服务是IIS,那么也许你可以省去http请求,而是直接检测IIS服务是否在运行