How to write a VBS Script to check a website and then stop and start a windows service
I need to write a VBS script to check a website for c开发者_StackOverflowertain text : "Service Unavailable" and "Error in /"
When it finds this I need it to restart the windows service "World wide publishing service"
Where do I start?
Any help would be much appreciated!
Cheers!
Andy
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)
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 page has 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
精彩评论